1 ImportJavax.crypto.Mac;2 ImportJavax.crypto.SecretKey;3 ImportJavax.crypto.spec.SecretKeySpec;4 5 Public classHMACSHA1 {6 7 Private Static FinalString mac_name = "HmacSHA1"; 8 Private Static FinalString ENCODING = "UTF-8"; 9 Ten /* One * shows a procedure for generating a specified algorithm key initializes the HMAC key A * @return - * @throws Exception - * the Public static String Initmackey () throws Exception { - //Get a key generator with the specified algorithm key - keygenerator keygenerator keygenerator =keygenerator.getinstance (mac_name); - //Generate a key + secretkey Secretkey =keygenerator.generatekey (); - return null; + } A */ at - /** - * Use HMAC-SHA1 signature method to sign Encrypttext - * @paramEncrypttext signed String - * @paramEncryptkey Key - * @return in * @throwsException - */ to Public Static byte[] Hmacsha1encrypt (String encrypttext, String encryptkey)throwsException + { - byte[] Data=encryptkey.getbytes (ENCODING); the //constructs a key based on a given byte array, and the second parameter specifies the name of a key algorithm *Secretkey Secretkey =Newsecretkeyspec (data, mac_name); $ //generates a Mac object that specifies the MAC algorithmPanax NotoginsengMac Mac =mac.getinstance (mac_name); - //Initializes a Mac object with a given key the Mac.init (Secretkey); + A byte[] Text =encrypttext.getbytes (ENCODING); the //Complete MAC Operation + returnmac.dofinal (text); - } $}
Signing data using the HMACSHA1 algorithm (Java edition)