3

I have to apply SHA256 algorithm to a string. As the Salesforce document says, I have used the method SHA256('myString', 'UTF-8') and its giving an error as 'Method not exists'

1
  • Are you trying to do it via Apex or in Marketing Cloud? They are entirely different technologies.
    – Adrian Larson
    Dec 13, 2019 at 5:51

2 Answers 2

4

Adrian's answer was very helpful in pointing me in the right direction, but the response is

System.StringException: BLOB is not a valid UTF-8 string

There is something in the blob returned by Crypto.generateDigest() that is not UTF-8.

Thanks to Mohith Shrivastava at https://cloudyworlds.blogspot.com/2014/01/encrypting-xml-response-from-external.html I was able to convert to hex to get a valid string output:

Blob myBlob = Blob.valueOf('Come on you mighty port');
Blob mySha = Crypto.generateDigest('SHA-256', myBlob);
//String myString = mySha.toString(); - Generates 'BLOB is not valid UTF-8'
String myHex = EncodingUtil.convertToHex(mySha);
System.debug(myHex);
// Converting hex back to string generates 'BLOB is not valid UTF-8' again
//String myString = EncodingUtil.convertFromHex(myHex).toString();

In my use case, the content of the string is not important, I'm just looking to create a consistent, unique string from a longer string so as not to exced 255 char limit.

See docs:

Blob Class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_blob.htm?search_text=crypto%20class#apex_methods_system_blob

Crypto Class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_crypto.htm?search_text=crypto%20class#apex_classes_restful_crypto

EncodingUtil Class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm?search_text=crypto%20class#apex_classes_restful_encodingutil

1

If you want to get the String hash of another String, you need to use the Crypto class, specifically the generateDigest method:

generateDigest(algorithmName, input)
Computes a secure, one-way hash digest based on the supplied input string and algorithm name.

Signature

public static Blob generateDigest(String algorithmName, Blob input)

Parameters
algorithmName
Type: String
Valid values for algorithmName are:

  • MD5
  • SHA1
  • SHA-256
  • SHA-512

input
Type: Blob

Return Value
Type: Blob

Example

Blob targetBlob = Blob.valueOf('ExampleMD5String');
Blob hash = Crypto.generateDigest('MD5', targetBlob);

So if you need to go String to String, it would look something like:

public static String getSha256(String input)
{
    return Crypto.generateDigest('SHA-256', Blob.valueOf(input)).toString();
}
2
  • Thanks for the reply Adrian.Blob to blob is working fine.But for string ,it giving a error as Blob.valueOf() is not a valid method
    – AMDB6
    Dec 13, 2019 at 6:53
  • Did you name one of your variables somewhere blob?
    – Adrian Larson
    Dec 13, 2019 at 14:05

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .