Cryptography 如何在Salesforce Apex中创建HMACSHA256 api签名

Cryptography 如何在Salesforce Apex中创建HMACSHA256 api签名,cryptography,salesforce,Cryptography,Salesforce,有人可以帮助在apex中使用crypto类创建HMACSHA256 api签名吗。下面给出了相应的java代码:- public static void main(String[] args) throws GeneralSecurityException, IOException { String secretKey = "secretKey"; String salt = "0123456789"; String generateHmacSHA256Signatu

有人可以帮助在apex中使用crypto类创建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);
    }
}
提前感谢

我想这对你来说行吗

复制到此处供后代使用(以防链接失效) AKK的答复: “Re:如何创建HMACSHA256 api签名 ‎2012年12月28日凌晨02:58

很抱歉没有格式化的代码,实际上我正在研究如何格式化,但在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);
}
谢谢“