July 14, 2012

Generating Base-64 SHA-1 Signature Header for Rackspace API in Apex

This post shares small code snippet to generate base-64 SHA1 Hash, which is typically used in passing as signature header to some APIs. We tested this snippet with Rackspace API Signature Header, which requires a base-64 SHA1 Hash.

The key point to note here is we need to encode the binary hash, not the hex hash, into base-64 and thus the resulting string should be 28 characters long. Here is the gist snippet to generate the hash

This point is important to remember because developers sometimes pick the HEX conversion approach for base 64 encoding, as this is available in examples for SHA1 in Apex Developer Guide.

Hope this saves your time in case you require a base-74 SHA1 Hash in similar format.