开发者

SHA256 implementation using Base64 for input and output

开发者 https://www.devze.com 2023-03-17 17:08 出处:网络
I\'ve been asked to develop the company\'s backoffice for the iPad and, while developing the login screen, I\'ve ran into an issue with the authentication process.

I've been asked to develop the company's backoffice for the iPad and, while developing the login screen, I've ran into an issue with the authentication process.

The passwords are concatenated with a salt, hashed using SHA-256 and stored in the database. The backoffice is Flash-based and uses the as3crypto library to hash then password+salt and my problem is that the current implementation uses Base64 for both input and output.

This site demonstrates how this can be done: just select Hash and select Base64 for both input and output format and fire away. So far, all my attempts have yielded different results from the ones this site (and the backoffice code) give me.

While I think that 开发者_如何学运维in theory it should be relatively simply:

  1. Base64 encode the pass+salt
  2. Hash it using SHA-256
  3. Base64 encode the result again

so far I haven't been able to do this and I'm getting quite the headache to be honest. My code is becoming a living maze, i'll have to redo-it tomorrow I reckon.

Any ideas? Cheers and thanks in advance

PS: Here's the Backoffice's Flash code for generating hashed passwords by the way:

var currentResult:ByteArray;
var hash:IHash = Crypto.getHash('sha256');
var data:ByteArray = Base64.decodeToByteArray(str + vatel);
currentResult = hash.hash(data);
return Base64.encodeByteArray(currentResult).toString();


The backoffice code does not do

  1. Base64 encode the pass+salt
  2. Hash it using SHA-256
  3. Base64 encode the result again

(as you wrote above)

Instead, what it does is

  1. Base64 decode the pass+salt string into a byte array
  2. Hash the byte array using SHA-256
  3. Base64 encode the byte array, returning a string

As per step 1 above, it's a unclear what kind of character encoding the input strings uses. You need to make sure that both systems use the same encoding for the input strings! UTF8, UTF16-LE or UTF16-BE makes a world of a difference in this case!

Start by finding out the correct character encoding to use on the iOS side.

Oh, and Matt Gallagher has written an easy to use wrapper class for hashes to use on iOS, HashValue.m, I've used it with good results.

0

精彩评论

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