Last active
September 7, 2017 01:28
-
-
Save liuxd/b869af39ad692aa8dda6965b21ba651c to your computer and use it in GitHub Desktop.
[SignGenerator] 生成API签名算法。
This file contains hidden or 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
<?php | |
/** | |
* 签名生产算法。 | |
* @param string $sClientID APP身份ID。 | |
* @param string $sUserToken APP生产的可以获得用户标识的令牌。 | |
* @param string sSecretKey 秘钥。 | |
* @return string | |
*/ | |
function getSign($sClientID, $sUserToken, $sSecretKey) | |
{ | |
$tmp1 = base64_encode($sClientID . $sUserToken); | |
$tmp2 = substr($tmp1, 0, -2); | |
$tmp3 = strrev($tmp2 . $sSecretKey); | |
$sign = md5($tmp3); | |
return $sign; | |
} |
This file contains hidden or 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
require 'base64' | |
require 'digest/md5' | |
def get_sign client_id, user_token, secret_key | |
tmp1 = Base64.encode64(client_id.to_s + user_token).strip | |
tmp2 = tmp1[0..tmp1.length - 3] | |
tmp3 = (tmp2 + secret_key).reverse | |
Digest::MD5.hexdigest tmp3 | |
end |
This file contains hidden or 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 javax.xml.bind.DatatypeConverter; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
public class SignGenerator | |
{ | |
public static void main(String a[]) | |
{ | |
// Demo | |
String client_id = "liuxd"; | |
String user_token = "23343we356"; | |
String secret_key = "15fc7a0b4f050989554e3c9cadc693fd"; | |
String sign = getSign(client_id, user_token, secret_key); | |
System.out.println(sign); | |
} | |
private static String getSign(String client_id, String user_token, String secret_key) | |
{ | |
String str = client_id + user_token; | |
byte[] message = str.getBytes(); | |
String tmp1 = DatatypeConverter.printBase64Binary(message); | |
String tmp2 = tmp1.substring(0, tmp1.length() - 2); | |
String tmp3 = tmp2 + secret_key; | |
StringBuffer tmp4 = new StringBuffer(tmp3); | |
StringBuffer tmp5 = tmp4.reverse(); | |
String tmp6 = tmp5.toString(); | |
String tmp7 = MD5(tmp6); | |
return tmp7; | |
} | |
private static String MD5(String sourceStr) | |
{ | |
String result = ""; | |
try { | |
MessageDigest md = MessageDigest.getInstance("MD5"); | |
md.update(sourceStr.getBytes()); | |
byte b[] = md.digest(); | |
int i; | |
StringBuffer buf = new StringBuffer(""); | |
for (int offset = 0; offset < b.length; offset++) { | |
i = b[offset]; | |
if (i < 0) { | |
i += 256; | |
} | |
if (i < 16) { | |
buf.append("0"); | |
} | |
buf.append(Integer.toHexString(i)); | |
} | |
result = buf.toString(); | |
} catch (NoSuchAlgorithmException e) { | |
System.out.println(e); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment