Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
墨香年少

hmacsha1签名方法,PHP,JAVA,GO

Recommended Posts

php排序

function ascii_params($params = array())
{
    if (!empty($params)) 
    {
        $p = ksort($params);
        if ($p) 
        {
            $str = '';
            foreach ($params as $k => $val) 
            {
                $str .= $k . '=' . $val . '&';
            }
            $strs = rtrim($str, '&');
            return $strs;
        }
    }
    return '';
}
function sign($data, $key)
{
    $str = $this->ascii_params($data);
    $signature = "";
    if (function_exists('hash_hmac')) 
    {
        $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
    }
    else
    {
        $blocksize = 64;
        $hashfunc = 'sha1';
        if (strlen($key) > $blocksize) 
        {
            $key = pack('H*', $hashfunc($key));
        }
        $key = str_pad($key, $blocksize, chr(0x00));
        $ipad = str_repeat(chr(0x36), $blocksize);
        $opad = str_repeat(chr(0x5c), $blocksize);
        $hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $str))));
        $signature = base64_encode($hmac);
    }
    return $signature;
}
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;

public class HmacUtil 
{

    public static final String HMAC_SHA1 = "HmacSHA1";
    public static final String HMAC_MD5 = "HmacMD5";
    public static final String HMAC_SHA256 = "HmacSHA256";
    public static final String HMAC_SHA512 = "HmacSHA512";
 
    public static String encrypt(String input, String key, String algorithm) {
        String cipher = "";
        try {
            byte[] data = key.getBytes(StandardCharsets.UTF_8);
            SecretKey secretKey = new SecretKeySpec(data, algorithm);
            Mac mac = Mac.getInstance(algorithm);

            mac.init(secretKey);
            byte[] text = input.getBytes(StandardCharsets.UTF_8);
            byte[] encryptByte = mac.doFinal(text);
            
            cipher = Base64.getEncoder().encodeToString(encryptByte);
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            e.printStackTrace();
        }
        return cipher;
    }
 

    public static String bytesToHexStr(byte[] bytes) {
        StringBuilder hexStr = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(b & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            hexStr.append(hex);
        }
        return hexStr.toString();
    }

    public static void main(String[] args) {

        String valSha1 = HmacUtil.encrypt("amount=100&appid=123456&notify_url=http://www.google.com&order_no=abcd", "keykeykey", HmacUtil.HMAC_SHA1);
        System.out.println(valSha1); //MM2yDl0IpBi8INXKDUSRba8cuVE=
    }
}

go:

package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
)

func main() {
	fmt.Println(GetSignature("amount=60.00&appid=111&order_no=222&timestamp=333&version=2.0", "444"))
}

func GetSignature(input, key string) string {
	key_for_sign := []byte(key)
	h := hmac.New(sha1.New, key_for_sign)
	h.Write([]byte(input))
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

注册必须使用2-8个中文汉字作为账号