WeChat two signature algorithms MD5 and HMAC-SHA256

In the process of developing the WeChat interface, sometimes I find that the signature verification fails. The same signature logic is that some interfaces run through, and I found a lot of pits after looking for a circle;

It turns out that the signType signature types of some interfaces are different. Some interfaces require MD5 for signType, and some require HMAC-SHA256. In fact, these two algorithms are both

There is not much difference in the hash algorithm. Difference, but the generated signature result will be different;

Attach the HMAC-SHA256 algorithm code below

package com.lh.micro.datasource.util;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HMACSHA256 {


    /**
     * Convert the encrypted byte array into a string
     *
     * @param b byte array
     * @return string
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n 

  

Historical article:
JAVA WeChat enterprise payment to change (ten minutes to get)
Methods and steps for WeChat authorization to obtain user openId
One WeChat account supports multiple environment webpage authorization at the same time
Two signature algorithms of WeChat MD5 and HMAC-SHA256

package com.lh.micro.datasource.util;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HMACSHA256 {


    /**
     * Convert the encrypted byte array into a string
     *
     * @param b byte array
     * @return string
     */
    public static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n 

Leave a Comment

Your email address will not be published.