{"id":2400,"date":"2026-10-20T13:00:00","date_gmt":"2026-10-20T07:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/"},"modified":"2026-11-15T11:30:03","modified_gmt":"2026-11-15T06:00:03","slug":"apex-crypto-encoding","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/","title":{"rendered":"Apex Crypto and Encoding: Hashing, AES and Base64 Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Apex Crypto gives you hashing, message signatures and AES encryption inside Salesforce. The most common real use is verifying that a webhook really came from your partner, by recomputing a signature over the body. So you can trust incoming data instead of hoping.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post builds on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-secure-coding-xss-csrf-injection\/\">Apex secure coding<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/named-credentials-in-apex-callouts\/\">Named Credentials<\/a>. We will verify a payment webhook for Bright Path Tutors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Apex Crypto?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Crypto is the <code>Crypto<\/code> class plus <code>EncodingUtil<\/code>. Crypto handles digests, message authentication codes and AES encryption. EncodingUtil converts between Blob and text formats such as Base64 and hexadecimal.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a wax seal on a letter. Anyone can read the letter, but the seal proves who sent it and that nobody opened it on the way. A signature works the same way for data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Apex Crypto Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Integrations send data over the internet, and anyone can post to a public endpoint. Without a check, a fake request looks exactly like a real one.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Crypto lets you verify the sender cheaply. It also covers hashing values for comparison and encrypting a field you must read back later, which Shield Platform Encryption does not cover for every case.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Apex Crypto Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A signature check follows the same five steps every time.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-1-1024x640.png\" alt=\"Apex Crypto diagram showing how a webhook signature is verified\" class=\"wp-image-2398\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">How a webhook signature is verified in Apex.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Hashing a Value<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A digest turns any input into a fixed-length Blob. It is one way, so you cannot get the original back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Blob input = Blob.valueOf('PaymentRef-9981');\nBlob digest = Crypto.generateDigest('SHA-256', input);\nSystem.debug(EncodingUtil.convertToHex(digest));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The log shows a 64 character hexadecimal string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4f8b2c1d9e...a07c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Supported algorithms are MD5, SHA1, SHA-256 and SHA-512. Use SHA-256 or stronger, since MD5 and SHA1 are broken for security use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a Signature with HMAC<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A plain hash proves nothing about who sent it, because anyone can compute it. A MAC mixes in a shared secret key.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String body = '{\"ref\":\"9981\",\"amount\":5000}';\nBlob key = Blob.valueOf(PaymentConfig__mdt.getInstance('Default').Secret__c);\nBlob mac = Crypto.generateMac('hmacSHA256', Blob.valueOf(body), key);\nString signature = EncodingUtil.base64Encode(mac);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only someone holding the same key can produce that signature.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comparing Safely<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Comparing strings with <code>==<\/code> can leak timing information. Apex Crypto gives you a constant time comparison.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Boolean ok = Crypto.verifyHMac('hmacSHA256', Blob.valueOf(body), key,\n                               EncodingUtil.base64Decode(headerSignature));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This returns true only when the signature matches, and it takes the same time either way.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Encrypting and Decrypting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">AES encryption is two way, so you can read the value back.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Blob key = Crypto.generateAesKey(256);\nBlob clear = Blob.valueOf('Account 1234');\nBlob secret = Crypto.encryptWithManagedIV('AES256', key, clear);\nBlob back = Crypto.decryptWithManagedIV('AES256', key, secret);\nSystem.debug(back.toString());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The output returns the original text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Account 1234<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The managed IV versions add a random initialisation vector for you, which is the safer choice unless you must match an external system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Apex Crypto Method Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This table lists the methods you will reach for.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td>Crypto.generateDigest<\/td><td>One way hash of data<\/td><\/tr><tr><td>Crypto.generateMac<\/td><td>Signature using a shared key<\/td><\/tr><tr><td>Crypto.verifyHMac<\/td><td>Safe signature comparison<\/td><\/tr><tr><td>Crypto.generateAesKey<\/td><td>Create a random AES key<\/td><\/tr><tr><td>Crypto.encryptWithManagedIV<\/td><td>Encrypt with a random IV<\/td><\/tr><tr><td>Crypto.decryptWithManagedIV<\/td><td>Decrypt data encrypted that way<\/td><\/tr><tr><td>EncodingUtil.base64Encode<\/td><td>Blob to Base64 text<\/td><\/tr><tr><td>EncodingUtil.convertToHex<\/td><td>Blob to hexadecimal text<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Hashing vs Encryption<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">People mix these up constantly. This table separates them.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Hashing<\/th><th>Encryption<\/th><\/tr><\/thead><tbody><tr><td>Reversible<\/td><td>No<\/td><td>Yes, with the key<\/td><\/tr><tr><td>Key required<\/td><td>Only for a MAC<\/td><td>Always<\/td><\/tr><tr><td>Output length<\/td><td>Fixed<\/td><td>Grows with input<\/td><\/tr><tr><td>Used for<\/td><td>Integrity and signatures<\/td><td>Confidentiality<\/td><\/tr><tr><td>Example<\/td><td>Verify a webhook<\/td><td>Store a token you must reuse<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-2-1024x640.png\" alt=\"Apex Crypto comparison of hashing and encryption\" class=\"wp-image-2399\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Hashing compared with encryption in Apex Crypto.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, hash when you only need to prove nothing changed. Encrypt when you must read the value back later.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Real Apex Crypto Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Bright Path receives payment confirmations from a gateway. Each request carries a signature header, and the endpoint must reject anything that does not match.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Store the Key Safely<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Keep the secret in protected Custom Metadata, never in the class file.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Where<\/th><th>Suitable<\/th><\/tr><\/thead><tbody><tr><td>Protected Custom Metadata<\/td><td>Yes, hidden from subscribers<\/td><\/tr><tr><td>Named Credential<\/td><td>Yes, for outbound auth<\/td><\/tr><tr><td>Custom Setting<\/td><td>Weak, many users can read it<\/td><\/tr><tr><td>Hard-coded in Apex<\/td><td>Never, it sits in source control<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Verify the Request<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@RestResource(urlMapping='\/payment\/*')\nglobal with sharing class PaymentWebhook {\n\n    @HttpPost\n    global static void receive() {\n        RestRequest req = RestContext.request;\n        String body = req.requestBody.toString();\n        String sent = req.headers.get('X-Signature');\n\n        if (String.isBlank(sent)) {\n            RestContext.response.statusCode = 401;\n            return;\n        }\n        Blob key = Blob.valueOf(PaymentConfig__mdt.getInstance('Default').Secret__c);\n        Boolean ok = Crypto.verifyHMac('hmacSHA256',\n                                       Blob.valueOf(body),\n                                       key,\n                                       EncodingUtil.base64Decode(sent));\n        if (!ok) {\n            RestContext.response.statusCode = 401;\n            return;\n        }\n        PaymentService.record(body);\n        RestContext.response.statusCode = 200;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The record is only created after the signature passes, so a forged call changes nothing.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Test Both Paths<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@isTest\nprivate class PaymentWebhookTest {\n    @isTest static void rejectsBadSignature() {\n        RestRequest req = new RestRequest();\n        req.requestBody = Blob.valueOf('{\"ref\":\"1\"}');\n        req.headers.put('X-Signature', 'd3Jvbmc=');\n        RestContext.request = req;\n        RestContext.response = new RestResponse();\n\n        Test.startTest();\n        PaymentWebhook.receive();\n        Test.stopTest();\n\n        System.assertEquals(401, RestContext.response.statusCode);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Always test the rejection path, because that is the one protecting you.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Apex Crypto<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Hard-coding the secret key in the Apex class.<\/li>\n\n<li>Comparing signatures with ==, which can leak timing details.<\/li>\n\n<li>Using MD5 or SHA1 for anything security related.<\/li>\n\n<li>Reusing one initialisation vector across messages.<\/li>\n\n<li>Storing an AES key in a readable custom setting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Why Hashing Is Not Encryption<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A hash cannot be reversed, so it is useless for anything you must read back. If someone suggests hashing a phone number so it can be shown later, that will not work.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Apex Crypto<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use SHA-256 or SHA-512 for digests.<\/li>\n\n<li>Use verifyHMac rather than comparing strings yourself.<\/li>\n\n<li>Keep keys in protected Custom Metadata or Named Credentials.<\/li>\n\n<li>Prefer the managed IV methods unless an external system dictates otherwise.<\/li>\n\n<li>Consider Shield Platform Encryption before writing your own field encryption.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a method that takes a text body and a key, and returns a Base64 HMAC SHA-256 signature. Hint: Blob.valueOf, Crypto.generateMac and EncodingUtil.base64Encode.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Apex Crypto Interview Questions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Apex Crypto questions appear in security focused developer interviews.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Apex Crypto Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: Which class handles encryption in Apex?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Apex Crypto work uses the Crypto class, with EncodingUtil for Base64 and hexadecimal conversion.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What does generateDigest do?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It creates a one way hash of a Blob, using an algorithm such as SHA-256.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What is the difference between a hash and a MAC?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A hash needs no key, so anyone can compute it. A MAC mixes in a shared secret, which proves who created it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What is Base64 used for?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Turning binary data into text that is safe to put in a header, a JSON body or a URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: Which algorithms does generateDigest support?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">MD5, SHA1, SHA-256 and SHA-512, though only the last two are safe for security.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: How do you create an AES key?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Call Crypto.generateAesKey with 128, 192 or 256 bits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: What does encryptWithManagedIV add?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A random initialisation vector, which Salesforce generates and stores with the output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: Is hashing reversible?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. You cannot recover the input, which is exactly why it suits signatures.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Intermediate Apex Crypto Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: Why use verifyHMac instead of comparing strings?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because it compares in constant time, so an attacker cannot learn the key from response timing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Where should an API secret be stored?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In protected Custom Metadata or a Named Credential. Never in the class or a readable custom setting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: What is an initialisation vector?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A random block mixed into encryption so the same input does not always produce the same output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: Can Apex Crypto replace Shield Platform Encryption?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Shield encrypts fields at rest transparently, while Apex Crypto encrypts values you manage yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: What is the heap cost of encryption?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Blobs count against heap, so encrypting large files in one transaction can hit the limit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you convert a Blob to readable text?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use toString for plain text, or EncodingUtil.base64Encode and convertToHex for binary data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: What is a salt and does Apex add one?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A salt is random data added before hashing so identical inputs differ. Apex does not add one, so you append it yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: Can you decrypt data encrypted outside Salesforce?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, if you use the same algorithm, key and IV. So you must agree the exact scheme with the other system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Apex Crypto Interview Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Scenario: a partner webhook must be verified. Walk through your design.<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Read the signature header, fetch the key from protected metadata, recompute the HMAC over the raw body and call verifyHMac. Process only on success, and return 401 otherwise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: Why must you sign the raw body, not a parsed object?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because parsing and re-serialising can change spacing or key order, which breaks the signature.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How do you rotate a shared key without downtime?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Accept either the old or the new key for a short window, then remove the old one once the partner has switched.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: What are the risks of writing your own field encryption?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Key management, searchability and reporting all break. Therefore Shield is usually the better answer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you prevent replay attacks?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Include a timestamp and a unique id in the signed payload, reject old timestamps and store processed ids.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q22: Why is MD5 still in the API?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For legacy checksum work, not security. So use it only when an old system requires that exact format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q23: How does Apex Crypto fit with Platform Events?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can sign the payload before publishing, so a subscriber in another system can verify it came from your org.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q24: What limits apply to Crypto calls?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is no separate crypto limit, but CPU time and heap still apply. So avoid encrypting large volumes in one transaction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q25: Does Apex Crypto work in a Lightning Web Component?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not directly, because it is server side. So the component calls an Apex method, and the crypto runs there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q26: Can Apex Crypto hash a password for login?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No, and you should never try. Salesforce handles authentication itself, so rolling your own login is both unsafe and unsupported.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q27: How large a payload can Apex Crypto handle?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is bound by the heap limit, which is 6 MB synchronous and 12 MB asynchronous. Therefore sign hashes of large files rather than the files themselves.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q28: Scenario: two systems produce different signatures for the same body. What do you check?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The exact bytes being signed, the character encoding, the key format and whether one side trimmed whitespace. Usually the body differs by one character.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q29: Is Base64 a form of encryption?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Base64 only changes how bytes are written, and anyone can decode it. So never use it to protect a secret.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_classes_restful_crypto.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: Crypto Class<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexref.meta\/apexref\/apex_classes_restful_encodingUtil.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Apex Reference: EncodingUtil Class<\/a><\/li>\n\n<li><a href=\"https:\/\/help.salesforce.com\/s\/articleView?id=sf.security_pe_overview.htm&amp;type=5\" target=\"_blank\" rel=\"noopener noreferrer\">Salesforce Help: Shield Platform Encryption<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Apex Crypto covers hashing, MAC signatures and AES encryption.<\/li>\n\n<li>Hashing is one way, while encryption can be reversed with the key.<\/li>\n\n<li>Use verifyHMac for safe signature comparison.<\/li>\n\n<li>Keep keys in protected Custom Metadata, never in code.<\/li>\n\n<li>Prefer Shield Platform Encryption for encrypting stored fields.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we start the Visualforce block with <a href=\"https:\/\/swapnilzambare.com\/blog\/visualforce-basics-standard-controllers\/\">Visualforce Basics and Standard Controllers<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apex Crypto gives you hashing, message signatures and AES encryption inside Salesforce. The most common real use is verifying that a webhook really came&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2397,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96,244],"tags":[68,98,17],"swapz_series":[],"class_list":["post-2400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","category-security-design-patterns","tag-advanced","tag-apex","tag-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apex Crypto: Hashing, AES and Base64 Guide<\/title>\n<meta name=\"description\" content=\"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apex Crypto: Hashing, AES and Base64 Guide\" \/>\n<meta property=\"og:description\" content=\"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-20T07:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-15T06:00:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Apex Crypto and Encoding: Hashing, AES and Base64 Explained\",\"datePublished\":\"2026-10-20T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/\"},\"wordCount\":1672,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-crypto-encoding-featured.png\",\"keywords\":[\"Advanced\",\"Apex\",\"Security\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\",\"Security and Design Patterns\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/\",\"name\":\"Apex Crypto: Hashing, AES and Base64 Guide\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-crypto-encoding-featured.png\",\"datePublished\":\"2026-10-20T07:30:00+00:00\",\"dateModified\":\"2026-11-15T06:00:03+00:00\",\"description\":\"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-crypto-encoding-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/apex-crypto-encoding-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Apex Crypto featured image: code editor card with HMAC signature code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/apex-crypto-encoding\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apex Crypto and Encoding: Hashing, AES and Base64 Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apex Crypto: Hashing, AES and Base64 Guide","description":"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/","og_locale":"en_US","og_type":"article","og_title":"Apex Crypto: Hashing, AES and Base64 Guide","og_description":"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.","og_url":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-10-20T07:30:00+00:00","article_modified_time":"2026-11-15T06:00:03+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Apex Crypto and Encoding: Hashing, AES and Base64 Explained","datePublished":"2026-10-20T07:30:00+00:00","dateModified":"2026-11-15T06:00:03+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/"},"wordCount":1672,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png","keywords":["Advanced","Apex","Security"],"articleSection":["Salesforce","Salesforce Developer","Security and Design Patterns"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/","url":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/","name":"Apex Crypto: Hashing, AES and Base64 Guide","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png","datePublished":"2026-10-20T07:30:00+00:00","dateModified":"2026-11-15T06:00:03+00:00","description":"Learn Apex Crypto for hashing, HMAC signatures, AES encryption and Base64 encoding, with safe key storage and the most asked interview questions.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/apex-crypto-encoding-featured.png","width":2400,"height":1260,"caption":"Apex Crypto featured image: code editor card with HMAC signature code"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/apex-crypto-encoding\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Apex Crypto and Encoding: Hashing, AES and Base64 Explained"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2400"}],"version-history":[{"count":1,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2400\/revisions"}],"predecessor-version":[{"id":3202,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2400\/revisions\/3202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2397"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2400"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}