Package com.trade.com796;
Import java.security.InvalidKeyException;
Import java.security.NoSuchAlgorithmException;
Import Javax.crypto.Mac;
Import Javax.crypto.spec.SecretKeySpec; /** * 1. HMAC_SHA1 encoding results need to be converted into hex format * * 2. The implementation of Base64 in Java is inconsistent with PHP, where Java does not fill the = number at the end of the string to supplement the number of bytes by 8 of the integer * * 3. HMAC_SHA1 is not SHA1, HMAC_SHA1 is a * * @author LEI * * */public class HMACSHA1 {private static final String hmac_s that requires a shared secret key
HA1 = "HmacSHA1"; /** * Generate signature Data _HMACSHA1 encryption * @param data * @param key * encrypted using key * @throws in Validkeyexception * @throws nosuchalgorithmexception */public static string Getsignature (string data, string key) th
Rows Exception {byte[] keybytes = Key.getbytes ();
Constructs a key based on the given byte array.
Secretkeyspec Signingkey = new Secretkeyspec (keybytes, HMAC_SHA1);
Mac Mac = Mac.getinstance (HMAC_SHA1);
Mac.init (Signingkey);
byte[] Rawhmac = mac.dofinal (Data.getbytes ());
String hexbytes = Byte2hex (RAWHMAC);
return hexbytes; } pRivate static string Byte2hex (final byte[] b) {String HS = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {//returns the string representation of an integer parameter as a 16-ary (base 16) unsigned integer.
Stmp = (java.lang.Integer.toHexString (b[n] & 0xFF));
if (stmp.length () = = 1) {HS = HS + "0" + stmp;
else {HS = hs + stmp;
} return HS; ////**//* @param args////public static void main (string[] args) {//try {//System.out.println (HMACSHA1
. Getsignature ("abc", "abc"));
catch (Exception e) {//E.printstacktrace ();
// }
// }
}