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 ...
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 ...
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);
...
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 ...
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....
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',
...
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 ...
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 ...
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 ...
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,...
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 ...
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....
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 -,...
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 ...
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 ...
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&...
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, ...
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 = '...
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 ...
4
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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.
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(...
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(......
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
crypto × 127apex × 89
encryption × 40
decrypt × 13
jwt × 12
security × 8
integration × 7
encodingutil × 6
java × 5
marketing-cloud × 4
rest-api × 4
oauth2 × 4
oauth × 4
blob × 4
base64 × 4
api × 3
soap × 3
customsetting × 3
string × 3
ssl × 3
hashcode × 3
lightning-aura-components × 2
javascript × 2
controller × 2
callout × 2