Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
rbenditt
Streaming Star

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
0 Kudos
2 REPLIES 2
belltown
Roku Guru

Re: How to Get a sha256-Encoded String in base64?

Have you tried ba2.FromHexString() instead of ba2.FromAsciiString() ?
0 Kudos
rbenditt
Streaming Star

Re: How to Get a sha256-Encoded String in base64?

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
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.