Note that there are some explanatory texts on larger screens.

plurals
  1. POHEX representation of byte array
    primarykey
    data
    text
    <p>I feel pretty stupid asking this, but as I don't know the answer, I'm going ahead anyway. </p> <p>I"m trying out some authentication code and want to know why the byte array I get from Rfc2898DeriveBytes needs to be converted to HEX and back to a byte array again for it to correctly initialise my HMACSHA1 object. I feel like I am doing something silly, or simply missing something obvious.</p> <p>My client code is a javascript function based on <a href="http://code.google.com/p/crypto-js/" rel="noreferrer">crypto-js</a>;</p> <pre><code>var key256bit = Crypto.PBKDF2(passwordEntered, saltBytes, 32, { iterations: 1000 }); var hmacBytes = Crypto.HMAC(Crypto.SHA1, url, key256bit, { asBytes: true }); var base64Hash = Crypto.util.bytesToBase64(hmacBytes); </code></pre> <p>My Server side code is as follows;</p> <pre><code> Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, encoding.GetBytes(salt), 1000); byte[] key = rfc2898.GetBytes(32); // Don't think I should need to do this. // However, it wont work if I initialise HMACSHA1 // with the rfc2898.GetBytes(32) string test = ByteArrayToString(key); HMACSHA1 hmacSha1 = new HMACSHA1(encoding.GetBytes(test)); byte[] computedHash = hmacSha1.ComputeHash(encoding.GetBytes(requestUri)); string computedHashString = Convert.ToBase64String(computedHash); </code></pre> <p>My ByteArrayToString method which I nabbed from the web is;</p> <pre><code>private static string ByteArrayToString(byte[] ba) { StringBuilder hex = new StringBuilder(ba.Length * 2); foreach (byte b in ba) hex.AppendFormat("{0:x2}", b); return hex.ToString(); } </code></pre> <p>So I can see that I get 32 bytes from my call to <code>rfc2898.GetBytes(32)</code>. I converted that to HEX using the ByteArrayToString method to confirm it matches what I am seeing in my Javascript variable key256bit. Now my test variable is a string of length 64 and when I pass that to HMACSHA1's constuctor using encoding.GetBytes(test) it is a byte array of length 64.</p> <p>The doco on crypto-js is a bit lacking, but I thought the call to Crypto.PBKDF2 with a param of 32 and it was creating a key of 32 bytes long (or 256 bits).</p> <p>Any clarification is much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload