Signing data using the HMACSHA1 algorithm (Java edition)

Source: Internet
Author: User
Tags hmac

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.