【筆記】java與python HmacSHA256加密代碼


java

    public static String sign(String content,String appkey) {

        String result = null;
        try {
            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
            byte[] keyBytes = appkey.getBytes("UTF-8");
            hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, "HmacSHA256"));

            byte[] hmacSha256Bytes = hmacSha256.doFinal(content.getBytes("UTF-8"));
            result = new String(Base64.encodeBase64(hmacSha256Bytes), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

python

import hashlib
import hmac
import base64

def sign(value,key):
    j = hmac.new(key.encode(), value.encode(), digestmod=hashlib.sha256);
    ret = (base64.b64encode(j.digest()).decode())
    return ret

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM