Java HmacSHA256 encryption is interoperable with PHP, Golang language SHA256

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Recent work related to HmacSHA256 encryption, PHP colleague Consulting I said can not encrypt, in fact, the problem is very simple, recorded, convenient for those who are not careful code friends ^_^.

How to encrypt HmacSHA256 in Java:

public static String SignUp(String secretKey, String plain) {

	        byte[] keyBytes = secretKey.getBytes();
	        byte[] plainBytes = plain.getBytes();

	        try {
	            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
	            SecretKeySpec secret_key = new SecretKeySpec(keyBytes, "HmacSHA256");
	            byte[] hashs = sha256_HMAC.doFinal(plainBytes);
	            StringBuilder sb = new StringBuilder();
	            for (byte x : hashs) {
	                String b = Integer.toHexString(x & 0XFF);
	                if (b.length() == 1) {
	                    b = '0' + b;
//	          sb.append(String.format("{0:x2}", x));
	            return sb.toString();
	            // String hash =
	            // Base64.encodeToString(sha256_HMAC.doFinal(plainBytes),
	            // Base64.DEFAULT);
//	       return hash;
	        } catch (Exception e) {
	        return null;

It is broadly divided into these sections to analyze:

1. Get SHA256 Instances

2. Generate an encryption key

3. Initialize the SHA256 instance with this encryption key

4. Use this instance to encrypt the generated hash based on the provided string

4. Finally, the whole thing is to switch to 16 and then output the string.

The PHP section is simple

The best language in the world, right!!

function ($plain,$secretKey){
  return bin2hex(hash_hmac("sha256",utf8_encode($plain) , utf8_encode($secretKey), true));

For ease of comparison, I declare the function arguments to be the same name.

Note the Hash_hmac declaration in the PHP Documentation:

string hash_hmac (string $ algo, string $ data, string $ key [, bool $ raw_output = FALSE])

The name of the hash algorithm to use, for example: "md5", "sha256", "haval160,4", etc. For a list of supported algorithms, see hash_algos ().

The message to be hashed.

The key used when generating the message digest using HMAC.

raw_output is set to TRUE to output raw binary data and FALSE to output lowercase hexadecimal strings. 

Just because Java engineers pass parameters to PHP's parameters, which leads to PHP engineers? Monte b!

Golang Partial post-update

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: 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.