How to Get a sha256-Encoded String in base64?

Hello,

I’m trying to convert a string to a hash that is sha256 encoded, but represented as a base64 string.

I’ve been trying to use an roEVPDigest for this, but I’m stuck. I’ve tried this:


ba1 = CreateObject("roByteArray")
ba1.FromAsciiString(hashstr)
digest = CreateObject("roEVPDigest")
digest.Setup("sha256")
digest.Update(ba1)
hash = digest.Final()

' base64 encode the hash
ba2 = CreateObject("roByteArray") 
ba2.FromAsciiString(hash)
usableHash = ba2.ToBase64String()

This doesn’t work at all. The variable ‘hash’ is returned as a hex-encoded string. That messes everything up. I’ve also tried to base64 encode ba1 after the digest.update(ba1) call, but it appears that ba1 is still the same (untransformed by the sha256 encoding). What I really want out of the digest variable is a transformed roByteArray that I can then call .ToBase64String() on.

Any way to do this?

Thanks,

Ron

Have you tried ba2.FromHexString() instead of ba2.FromAsciiString() ?

No, I didn’t know that function was available but it makes so much sense now that you’ve mentioned it.

And yes, this solves my problem!!!

Thanks so much!

Ron