Home > Backend Development > PHP Tutorial > java - How does PHP convert a string to a binary array, then perform md5 digest of the binary array, and convert the digested binary array into a hexadecimal string for output?

java - How does PHP convert a string to a binary array, then perform md5 digest of the binary array, and convert the digested binary array into a hexadecimal string for output?

WBOY
Release: 2016-08-04 09:21:01
Original
1296 people have browsed it

Convert the input parameter string waitForSignString into a binary array, and use UTF-8 for encoding (Java code such as: waitForSignString.getByte("utf-8")) to obtain the binary format data of the signature input parameter (waitForSignBytes).

Digest signature for waitForSignBytes using MD5, Sha1 or Sha256 algorithm. Get signature in result binary array data (signatureBytes)

Conduct hexadecimal string encoding on the resulting binary array (signatureBytes) of the signature output to obtain the final signature string (signature)

Algorithm expression:

String signature = Hex(digest (stringToSign+secretKey)) The digest algorithm can be: MD5, Sha256Hex

How should this signature algorithm be implemented in PHP? PHP's md5 seems to have no way to operate on arrays. . . .

Reply content:

Convert the input parameter string waitForSignString into a binary array, and use UTF-8 for encoding (Java code such as: waitForSignString.getByte("utf-8")) to obtain the binary format data of the signature input parameter (waitForSignBytes).

Digest signature for waitForSignBytes using MD5, Sha1 or Sha256 algorithm. Get signature in result binary array data (signatureBytes)

Conduct hexadecimal string encoding on the resulting binary array (signatureBytes) of the signature output to obtain the final signature string (signature)

Algorithm expression:

String signature = Hex(digest (stringToSign+secretKey)) The digest algorithm can be: MD5, Sha256Hex

How should this signature algorithm be implemented in PHP? PHP's md5 seems to have no way to operate on arrays. . . .

  1. In PHP, strings are binary compatible and come with encoding by default. If your string itself is UTF-8 encoded, then it is already a UTF-8 binary array.

  2. The md5 function in PHP can directly calculate the MD5 value of a string (that is, a binary array), and the default output is the result that has been converted to hexadecimal.

So, what the Modo operation is implemented in Java...
In PHP, it is $md5 = md5('string');

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template