3

I am using the below code to get the HMAC SHA 256. But signature debug is not getting printed.

public void genrateSignature() {
    String salt = String.valueOf(Crypto.getRandomInteger());
    String secretKey = 'xjoavU2LaJ1vaGCx4Ekd';

    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);
    }
  • 1
    What is your debug log level for the ApexCode category? If it's lower than 'DEBUG' (so, INFO, WARN, ERROR, or NONE), then the 2-argument debug method won't be captured in the log. – Derek F Jun 16 '16 at 13:38
  • I don't see generateHmacSHA256Signature(salt, secretKey) in the Crypto class. Where does that method come from? – crmprogdev Jun 16 '16 at 13:46
  • Yes, it is 'DEBUG'. – Abhinav Bhatia Jun 16 '16 at 14:16
  • It is present in the first method generateHmacSHA256Signature(salt, secretKey). – Abhinav Bhatia Jun 16 '16 at 14:18
1

As Derek pointed out, this must be your debug level. Your code is fine.

Here, I just copy-pasted this out of my DE (this compiles):

global class HMAC {

    public static void generateSignature() {
        String salt = String.valueOf(Crypto.getRandomInteger());
        String secretKey = 'xjoavU2LaJ1vaGCx4Ekd';
        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);
    }
}

... run this in execute anonymous:

HMAC.generateSignature();

And observe your signature hits the debug log.

| improve this answer | |
1

A few years ago, I created this article on my blog (https://eltoroit.herokuapp.com/Blog.app?page=CryptoCode) which shows all the different ways to encrypt/decrypt in Apex.

public class CryptoSample {
    public void Demo(){
        Blob bKey;
        String encryptedData;
        String decryptedData;
        String initialData = 'the quick brown fox jumps over the lazy dog';

        // Working with keys...
        // Generate key ...
        bKey = GenerateAESKey(256);             
        System.debug('BKey: ' + bKey);
        // but need the String version to save it...
        String sKey = KeyToString(bKey);        
        System.debug('SKey: ' + sKey);
        // and the Blob version to use it...
        Blob b2 = keyFromString(sKey);
        //  OK, but make sure they are the same!
        System.assert(sKey == KeyToString(b2)); 

        // AES128
        bKey = GenerateAESKey(128);
        encryptedData = Encrypt('AES128', bKey, initialData);
        decryptedData = Decrypt('AES128', bKey, encryptedData);
        System.assertEquals(initialData, decryptedData);

        // AES192
        bKey = GenerateAESKey(192); 
        encryptedData = Encrypt('AES192', bKey, initialData);
        decryptedData = Decrypt('AES192', bKey, encryptedData);
        System.assertEquals(initialData, decryptedData);

        // AES256
        bKey = GenerateAESKey(256); 
        encryptedData = Encrypt('AES256', bKey, initialData);
        decryptedData = Decrypt('AES256', bKey, encryptedData);
        System.assertEquals(initialData, decryptedData);
    }

    private String Encrypt(String method, Blob bKey, String data) {
        System.debug('Encrypting: ' + Data);
        Blob bData = Blob.valueOf(data);
        Blob bEncrypted = Crypto.encryptWithManagedIV(method, bKey, bData);
        String output = EncodingUtil.base64Encode(bEncrypted);
        System.debug('Encrypted: ' + output);
        return output;
    }
    private String Decrypt(String method, Blob bKey, String secureData) {
        System.debug('Decrypting: ' + secureData);
        Blob bSecureData = EncodingUtil.base64Decode(secureData);
        Blob bData = Crypto.decryptWithManagedIV(method, bKey, bSecureData);
        String output = bData.toString();
        System.debug('Decrypted: ' + output);
        return output;
    }

    private Blob GenerateAESKey(Integer Size) {
        // Size values are 128, 192, 256
        return Crypto.generateAesKey(Size);
    }
    private String keyToString(Blob bKey) {
        return EncodingUtil.base64Encode(bKey);
    }
    private Blob keyFromString(String sKey) {
        return EncodingUtil.base64Decode(sKey);
    }
}

As mentioned by others, I think your code seems fine.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.