Created
January 13, 2022 09:45
-
-
Save beilly/46d18b5b822ba071fc3dbc0e0a443f8a to your computer and use it in GitHub Desktop.
oopay 签名, Kotlin 版本、Java 版本、JavaScript 版本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ibeilly.android.cards.common; | |
import java.io.UnsupportedEncodingException; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Map; | |
public class OOPaySign { | |
private static String encodingCharset = "UTF-8"; | |
/** | |
* 计算签名 | |
* @param map 参数Map | |
* @param key 商户秘钥 | |
* @return | |
*/ | |
public static String getSign(Map<String,Object> map, String key){ | |
ArrayList<String> list = new ArrayList<String>(); | |
for(Map.Entry<String,Object> entry:map.entrySet()){ | |
if(null != entry.getValue() && !"".equals(entry.getValue())){ | |
list.add(entry.getKey() + "=" + entry.getValue() + "&"); | |
} | |
} | |
int size = list.size(); | |
String [] arrayToSort = list.toArray(new String[size]); | |
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER); | |
StringBuilder sb = new StringBuilder(); | |
for(int i = 0; i < size; i ++) { | |
sb.append(arrayToSort[i]); | |
} | |
String result = sb.toString(); | |
result += "key=" + key; | |
log.info("signStr:{}", result); | |
result = md5(result, encodingCharset).toUpperCase(); | |
log.info("sign:{}", result); | |
return result; | |
} | |
public static String md5(String value, String charset) { | |
MessageDigest md = null; | |
try { | |
byte[] data = value.getBytes(charset); | |
md = MessageDigest.getInstance("MD5"); | |
byte[] digestData = md.digest(data); | |
return toHex(digestData); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
public static String toHex(byte input[]) { | |
if (input == null) { | |
return null; | |
} | |
StringBuffer output = new StringBuffer(input.length * 2); | |
for (int i = 0; i < input.length; i++) { | |
int current = input[i] & 0xff; | |
if (current < 16) { | |
output.append("0"); | |
} | |
output.append(Integer.toString(current, 16)); | |
} | |
return output.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const md5 = require('js-md5'); | |
/** | |
* 计算签名 | |
* @param map 参数Map | |
* @param key 商户秘钥 | |
* @return | |
*/ | |
function getSign(map, key) { | |
var arrayToSort = []; | |
for (let key1 in map) { | |
let value = map[key1]; | |
if (null != value && "" != value) { | |
arrayToSort.push(`${key1}=${value}&`); | |
} | |
} | |
arrayToSort.sort(); | |
var result = arrayToSort.join(''); | |
result += `key=${key}`; | |
console.log(`signStr:${result}`); | |
result = md5(result, ).toUpperCase(); | |
console.log(`sign:${result}`); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.UnsupportedEncodingException | |
import java.security.MessageDigest | |
import java.security.NoSuchAlgorithmException | |
import java.util.* | |
import kotlin.collections.ArrayList | |
fun main(args : Array<String>){ | |
val sign = getSign(mapOf( | |
"ewrwer" to "wieir", | |
"were" to "weiitr", | |
"Ar" to "weitr", | |
"yiuAr" to "wiiter", | |
), "Fdfgfghfhfgwrrwe") | |
println("Hello World!") | |
println("Hello :$sign") | |
} | |
/** | |
* 计算签名 | |
* @param map 参数Map | |
* @param key 商户秘钥 | |
* @return | |
*/ | |
fun getSign(map: Map<String, Any?>, key: String): String? { | |
val list = ArrayList<String>() | |
for ((key1, value) in map) { | |
if (null != value && "" != value) { | |
list.add("$key1=$value&") | |
} | |
} | |
val size: Int = list.size | |
val arrayToSort: Array<String> = list.toArray(arrayOfNulls(size)) | |
Arrays.sort(arrayToSort, java.lang.String.CASE_INSENSITIVE_ORDER) | |
val sb = StringBuilder() | |
for (i in 0 until size) { | |
sb.append(arrayToSort[i]) | |
} | |
var result = sb.toString() | |
result += "key=$key" | |
println("signStr:$result") | |
result = md5(result, "UTF-8")?.toUpperCase() ?: "" | |
println("sign:$result") | |
return result | |
} | |
fun md5(value: String, charset: String?): String? { | |
var md: MessageDigest? = null | |
return try { | |
val data = value.toByteArray(charset(charset!!)) | |
md = MessageDigest.getInstance("MD5") | |
val digestData: ByteArray = md.digest(data) | |
digestData.toHexString() | |
} catch (e: NoSuchAlgorithmException) { | |
e.printStackTrace() | |
null | |
} catch (e: UnsupportedEncodingException) { | |
e.printStackTrace() | |
null | |
} | |
} | |
fun ByteArray.toHexString() : String { | |
return this.joinToString("") { | |
java.lang.String.format("%02x", it) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OOPay
https://www.kancloud.cn/ethanzhou/honorpay/2470345