Turn: HmacMD5 algorithm [php version]

Source: Internet
Author: User
Tags hmac
Turn: HmacMD5 algorithm [php] PHP code

Function HmacMd5 ($ data, $ key) {// RFC 2104 HMAC implementation for php. // Creates an md5 HMAC. // Eliminates the need to install mhash to compute a HMAC // Hacked by Lance Rushing (NOTE: Hacked means written) // you need to configure the environment to support iconv, otherwise the Chinese parameter cannot be properly processed $ key = iconv ("GB2312", "UTF-8", $ key); $ data = iconv ("GB2312", "UTF-8", $ data ); $ B = 64; // byte length for md5 if (strlen ($ key)> $ B) {$ key = pack ("H *", md5 ($ key) ;}$ key = str_pad ($ key, $ B, chr (0x00); $ ipad = str_pad ('', $ B, chr (0x36); $ opad = str_pad ('', $ B, chr (0x5c); $ k_ipad = $ key ^ $ ipad; $ k_opad = $ key ^ $ opad; return md5 ($ k_opad. pack ("H *", md5 ($ k_ipad. $ data )));}

HMAC requires an encryption hash function (represented as H) and a key K.

Assume that H is a hash function that encrypts data blocks with a basic iterative compression function.

B indicates the length of the data block. (The split data block length of the hash function mentioned above is B = 64). use L to indicate the output data length of the hash function (L = 16, SHA? 1 in L = 20 ).

The key length can be any positive integer smaller than or equal to the length of the data block. If the key length used in the application is larger than that of B, hash is used first.
Function H acts on it, and then uses the L-length string output by H as the actual key used in HMAC.

Generally, the recommended minimum key K length is L. (The length of the output data is equal to that of H ).

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.