Hot answers tagged

23 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 ...
user avatar
22 votes

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

Just want to update this answer with the latest process, this is built referencing the Google Service Account oAuth instructions (look at REST code) (credit to Jai-Singh for original Salesforce code): ...
user avatar
  • 321
12 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 ...
user avatar
  • 747
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); ...
user avatar
  • 434k
10 votes
Accepted

Has anyone got the OAuth 2.0 JWT Bearer Token Flow to work initiated from Apex code now 'RSA-SHA256' signing is available?

I now have this working for the specific case of connecting back to Salesforce from Salesforce. So essentially it is a port of the Java sample code to Apex. So it appears that Apex's RSA-SHA256 is ...
user avatar
  • 130k
9 votes
Accepted

Storing hash values (blob) in a string field?

You need to turn the digest bytes into a valid string first and one way to do that is to base64 encode: Blob b = Crypto.generateDigest('SHA-256', Blob.valueOf(valueToHash)); sfMessage....
user avatar
  • 130k
7 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 ...
user avatar
6 votes

Securely Storing a static API secret/key in a Salesforce app

Besides the other question, with its accepted answer, see Secure Coding Storing Secrets for additional information. It's basically the same information summarized in that answer, but includes much ...
user avatar
  • 434k
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 ...
user avatar
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 ...
user avatar
  • 434k
6 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....
user avatar
  • 87k
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,...
user avatar
6 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', ...
user avatar
  • 2,334
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 ...
user avatar
  • 434k
5 votes
Accepted

How to decrypt AES-256 in node.js

I found this great post by Chuck Mortimore about a similar problem but when crypting in Salesforce and decrypting in Java. I was able to port this to node and it worked great. I hope this will be ...
user avatar
  • 710
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 ...
user avatar
  • 87k
4 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....
user avatar
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 ...
user avatar
  • 434k
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 = '...
user avatar
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, ...
user avatar
4 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 ...
user avatar
  • 6,225
4 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&...
user avatar
  • 48.3k
3 votes

How to create a signature using crypto class

The input for String key should be the PKCS#8 formatted Private key. If you follow the instructions on the blog you linked to, use the string of characters between the outputted -----BEGIN PRIVATE KEY-...
user avatar
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 ...
user avatar
  • 866
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....
user avatar
  • 3,813
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 ...
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.
user avatar
  • 434k
3 votes

How to store a private key in a protected custom setting for use in Crypto class?

The only decent option is to split the private key into separate parts (probably around 10) and save those into the fields. Then in your code, concatenate the parts into the complete key. I ...
user avatar
3 votes
Accepted

Crypto Encryption returns different output on every run

encyptWithManagedIV produces a random IV each run, which is prepended to the output. Use a specific IV if you want repeatable results. Note that decryptWithManagedIV knows how to pull the IV off the ...
user avatar
  • 434k
3 votes
Accepted

Generating HMAC message digest via Crypto class

You are encoding it wrong. Try below code: Blob blobSignature = Crypto.generateMac('hmacSHA256', Blob.valueOf('testmessage'), Blob.valueOf('123456789')); System.debug(EncodingUtil.convertToHex(...
user avatar
  • 22.2k

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