开发者

Saving a hashed string in Salesforce?

开发者 https://www.devze.com 2023-02-19 03:43 出处:网络
Not a SFDC expert but need to integrate a web form hosted in SFDC. For other reasons, this page needs to capture an initial password and send off the salted/sha256\'d version of the password to anothe

Not a SFDC expert but need to integrate a web form hosted in SFDC. For other reasons, this page needs to capture an initial password and send off the salted/sha256'd version of the password to another system or a human. I don't want to save the cleartext obviously.

What's the lightest touch way to hash the password before saving? I could do t开发者_如何学编程his in the browser except that I don't want to rely on Javascript.


Yes, you should add this code to an apex trigger.

trigger on sObject(before insert){
  for(sObject s: trigger.new){
    s.Hash__c = Crypto.generateDigest('SHA-256', Blob.valueOf(s.passwordString__c));
    s.passwordString__c = null;
  }
}


Here is the code to encrypt a string to SHA256 hash format :

//value to process
Blob data= Blob.valueOf('Any String');

or in case of field value

Blob data= Blob.valueOf(sObject.FieldAPIName));
Blob hash = Crypto.generateDigest('SHA-256', data);
//Convert SHA-256 Hash data to String
String encryptedString =EncodingUtil.convertToHex(hash);


Apex has a Crypto class which has a generateDigest method you need, something like:

Blob hash = Crypto.generateDigest('SHA-256', Blob.valueOf(passwordString));


This worked for me =>

String abc = EncodingUtil.convertToHex(
             Crypto.generateDigest('SHA-256', 
             Blob.valueOf('test String')));
0

精彩评论

暂无评论...
验证码 换一张
取 消