Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs
24 votes
Accepted

SHA1 Hash Results

You Should use this String shasign = EncodingUtil.convertToHex(Crypto.generateDigest('SHA1',Blob.valueOf(Your String))); Clarification -: base64Encode will simply convert a Blob to an unencoded ...
Tarachand Karwal's user avatar
13 votes

Connect apex and Google API using JWT to retrieve Oauth 2.0 token

As of Winter '17 there are new JWT methods baked in to Apex. One hoop you have to jump through is getting your key from Google into a Java keystore (JKS) file. Some general info on getting a JKS file ...
jon_wu's user avatar
  • 777
12 votes
Accepted

MD5 Blob Cannot Convert to String

Instead of toString, you need to actually use EncodingUtil.convertToHex to get the output you expect: String myString = 'Some String'; system.debug(myString); Blob myBlob = Blob.valueOf(myString); ...
sfdcfox's user avatar
  • 494k
9 votes
Accepted

Canvas app - verify signed request

Here is a step-by-step click path to create a Salesforce Canvas App that uses a POSTed Signed Request to a Visualforce Page with an Apex Controller (and the Apex Controller successfully validates the ...
Bryan Isbell's user avatar
7 votes
Accepted

Does Crypto.generateMac method work exactly the same as Mac and SecretKeySpec java classes

Your Apex code just misses the base64 decoding step on your secret key. If you change the call to generateMac() to this: Blob mac = Crypto.generateMac(algorithmName, Blob.valueOf(input), EncodingUtil....
David Reed's user avatar
  • 93.1k
7 votes
Accepted

How Verify HMAC in Salesforce

Instead of Blob.valueOf(hmacString) you need to use EncodingUtil.convertFromHex(hmacString) to generate the blob for the HMAC part. Boolean verified = Crypto.verifyHmac( 'hmacSHA256', ...
tugce's user avatar
  • 2,675
6 votes
Accepted

How to create RS256 signature using apex

DISCLAIMER: I'm not a crypto expert, this is an informed guess using the linked references. According to the Wikipedia article on JSON Web Tokens, Typical cryptographic algorithms used [for ...
Jason Clark's user avatar
  • 11.9k
6 votes
Accepted

Encryption and Decryption in apex Salesforce Using Crypto Class

The following code works in my org. Blob key = Crypto.generateAesKey(256); Blob source = Blob.valueOf('Hello World'); Blob encrypted = Crypto.encryptWithManagedIV('AES256', key, source); String ...
sfdcfox's user avatar
  • 494k
6 votes
Accepted

Using the Crypto.verify() method to verify a JWT signature

JWT is encoded using base64 url-safe scheme so you must decode the signature appropriately when you convert it into bytes, straight base64 decoding won't do. URL-safe base64 encoding is defined in JWS ...
identigral's user avatar
  • 7,644
6 votes

How to encrypt text in apex using sha512 algorighm?

As @DerekF pointed out in his comment, you are conflating Hashing and Encryption. SHA-512 is the 512-bit digest version of the SHA-2 hashing function. Per Wikipedia, a cryptographic hashing function,...
Jason Clark's user avatar
  • 11.9k
5 votes

JWT, Cyrpto.Sign and EncodingUtil.base64Encode

As JWT is meant to be url-safe, the 62nd and 63rd characters used in Base64 encoding (+ and /, respectively) which are not url-safe must be replaced with characters that are url safe. + is mapped to -,...
Derek F's user avatar
  • 62.1k
5 votes
Accepted

Integer Array to Base64 String

String.fromCharArray is going to create a UTF-8 encoded string, which will alter the bytes in the stream. For example, 242 will be encoded as two bytes instead of one. Any value outside 0-127 will be ...
sfdcfox's user avatar
  • 494k
5 votes

Connect apex and Google API using JWT to retrieve Oauth 2.0 token

For a properly formatted JWT to be generated, it's important to note that this line of code: JWT = JWT.replaceAll('=',''); Needs to be changed to this: JWT = JWT.replaceAll('=',''); JWT = JWT....
AngularNerd's user avatar
5 votes
Accepted

Prevent Duplicates using unique key

BUT can I rely on it ? Is there a possibility that 2 records with different keys would generate the same hash ? Yes, there is. There must be, because you're compressing more than 256 bits of data ...
David Reed's user avatar
  • 93.1k
5 votes
Accepted

How can we encrypt/decrypt AES-GCM (A256GCM) algorithm (i.e. JCA: AES/GCM/NoPadding) in Salesforce?

I don't think you'll be able to do this in Apex. Crypto is documented to use only CBC mode: These algorithms are all industry standard Advanced Encryption Standard (AES) algorithms with different ...
David Reed's user avatar
  • 93.1k
5 votes
Accepted

Encrypting and/or decrypting ciphertext with the provided Initialization Vector (IV)

You can't decrypt the same ciphertext (bodystring) in your example with both decryptWithManagedIV and decrypt methods, only one of them will work. decryptWithManagedIV assumes that the ciphertext ...
identigral's user avatar
  • 7,644
5 votes
Accepted

How to encrypt text in apex using sha512 algorighm?

There is a difference between hashing (which is what the SHA512 algroithm does) and encrypting. Hashing A hash is a one-way function (or it's supposed to be). You put data in, you get a "hash&...
Derek F's user avatar
  • 62.1k
4 votes

Two-Way SSL vs Crypto Class

You use SSL to secure data in motion, and the Crypto class to secure data at rest. Generally speaking, you want to secure communication between Salesforce and some other system, you use SSL; you can ...
sfdcfox's user avatar
  • 494k
4 votes

How do I get value of Tenant Secret Key by Apex Code?

Just to clarify, we do not provide the ability to our customers to encrypt arbitrary data with the shield keys themselves because it's not what we're trying to achieve. Shield, at least as of today, ...
Alexandre Hersans's user avatar
4 votes

How to generate a 32 byte private key?

Above error is thrown because the private key is not 32 bytes. The expected and actual private key byte calculation is mismatching. Recommended approach to resolve this String algorithmName = '...
Abhishek Daware's user avatar
4 votes

SHA256 Algorithm

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....
MikeA's user avatar
  • 1,095
3 votes
Accepted

Certificates - api or sobject access

As far as I can tell, there is no way to query the available certificates in Apex, the Partner API, or the Tooling API. They are exposed in the Metadata API, but don't seem to have a field that ...
Daniel Ballinger's user avatar
3 votes

MD5 Blob Cannot Convert to String

This is a useful cryptography helper class I often use: public class CryptoUtils { public static String SHA1Hash(String value) { return hash('SHA1', value); } public static ...
Robs's user avatar
  • 9,356
3 votes
Accepted

HmacSHA256 encoding in salesforce

Apparently I made a mistake in the conversion. So here's the right conversion method. The issue was that the HMC was a string representation of a HEX so I had to parse it down Here's the correct ...
raym0nd's user avatar
  • 886
3 votes
Accepted

LockerService and window.crypto.getRandomValues()

Yes, you are correct in that Locker did not properly handle those Objects. This was fixed in the .12 patch, which was released last week. This should be functioning as expected with Locker enabled now....
TrevorBliss's user avatar
  • 3,833
3 votes

JWT verify signature

As of now the Apex Crypto Class doesn't support the verify method. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_crypto.htm And most of the examples ...
Dhanusha Mahatantila's user avatar
3 votes
Accepted

How do I convert a Blob into an alphanumeric string and back?

Use EncodingUtil.base64encode to convert to a String, and base64decode to get back to a Blob.
sfdcfox's user avatar
  • 494k
3 votes
Accepted

convert javascrip function CryptoJS.HmacSHA256() to apex

The output of your JavaScript is hexadecimal text (Base 16). The output of your Apex is Base64. To generate a hexadecimal string in Apex, use EncodingUtil.convertToHex(blob) rather than base64Encode(...
David Reed's user avatar
  • 93.1k
3 votes
Accepted

Crypto.verify(..) - format of a public RSA key

RS256 (RSA Signature with SHA-256) is an asymmetric algorithm that uses a public/private RSA key pair to authenticate the signature. When verifying ciphertext produced via RSA-SHA256, Crypto.verify(......
identigral's user avatar
  • 7,644
3 votes

Is it possible to decrypt a message in APEX using asymmetric RSA algorithm?

You can only decrypt symmetric AES-based encryption. RSA is only used for digital signatures (Crypo.sign and Crypto.verify methods).
sfdcfox's user avatar
  • 494k

Only top scored, non community-wiki answers of a minimum length are eligible