0

有人可以帮助使用加密类在 apex 中创建 HMACSHA256 api 签名。相应的java代码如下: -

public static void main(String[] args) throws GeneralSecurityException, IOException {

    String secretKey = "secretKey";
    String salt = "0123456789";

    String generateHmacSHA256Signature = generateHmacSHA256Signature(salt, secretKey);
    System.out.println("Signature: " + generateHmacSHA256Signature);

    String urlEncodedSign = URLEncoder.encode(generateHmacSHA256Signature, "UTF-8");

    System.out.println("Url encoded value: " + urlEncodedSign);
}

public static String generateHmacSHA256Signature(String data, String key) throws GeneralSecurityException {
    byte[] hmacData = null;

    try {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(secretKey);
        hmacData = mac.doFinal(data.getBytes("UTF-8"));
        return new Base64Encoder().encode(hmacData);
    } catch (UnsupportedEncodingException e) {
        throw new GeneralSecurityException(e);
    }
}

提前致谢

4

1 回答 1

0

http://boards.developerforce.com/t5/Apex-Code-Development/How-to-create-HMACSHA256-api-signature/td-p/551055

我想这对你有用吗?

为后代复制(以防链接失效)AKK 的回答:“Re:如何创建 HMACSHA256 api 签名‎12-28-2012 02:58 AM

对于未格式化的代码,我很抱歉,实际上我正在研究如何格式化,但在 mozilla 中没有找到任何内容,并且出现了通过 chrome 编辑器登录时。

我使用下面的代码得到了正确的签名,也许这对某人有帮助:-

public void genrateSignature() {
    String salt = String.valueOf(Crypto.getRandomInteger());
    String secretKey = 'secret_key';
    String signature = generateHmacSHA256Signature(salt, secretKey);
    System.debug('Signature : '+signature);
}

private static String generateHmacSHA256Signature(String saltValue, String secretKeyValue) {
    String algorithmName = 'HmacSHA256';
    Blob hmacData = Crypto.generateMac(algorithmName, Blob.valueOf(saltValue), Blob.valueOf(secretKeyValue));
    return EncodingUtil.base64Encode(hmacData);
}

谢谢”

于 2013-09-21T01:14:09.563 回答