public byte[] Hash(byte[] clearBytes) { using (var hmac = new HMACSHA512(_key)) { hmac.Initialize(); byte[] hashBytes = hmac.ComputeHash(clearBytes); return hashBytes; } }
// This function is defined as follow : // Func (S, i) = HMAC(S || i) | HMAC2(S || i) | ... | HMAC(iterations) (S || i) // where i is the block number. private byte[] Func() { byte[] INT_block = Int(m_block); m_HMACSHA512.TransformBlock(m_salt, 0, m_salt.Length, m_salt, 0); m_HMACSHA512.TransformFinalBlock(INT_block, 0, INT_block.Length); byte[] temp = m_HMACSHA512.Hash; m_HMACSHA512.Initialize(); byte[] ret = temp; for (int i = 2; i <= m_iterations; i++) { temp = m_HMACSHA512.ComputeHash(temp); for (int j = 0; j < BlockSize; j++) { ret[j] ^= temp[j]; } } // increment the block count. m_block++; return(ret); }
/// <summary> /// NetAESEncryption constructor /// </summary> public NetAESEncryption(string key, int bitsize) { if (!m_keysizes.Contains(bitsize)) throw new NetException(string.Format("Not a valid key size. (Valid values are: {0})", NetUtility.MakeCommaDelimitedList(m_keysizes))); byte[] entropy = Encoding.UTF32.GetBytes(key); // I know hardcoding salts is bad, but in this case I think it is acceptable. HMACSHA512 hmacsha512 = new HMACSHA512(Convert.FromBase64String("i88NEiez3c50bHqr3YGasDc4p8jRrxJAaiRiqixpvp4XNAStP5YNoC2fXnWkURtkha6M8yY901Gj07IRVIRyGL==")); hmacsha512.Initialize(); for (int i = 0; i < 1000; i++) { entropy = hmacsha512.ComputeHash(entropy); } int keylen = bitsize / 8; m_key = new byte[keylen]; Buffer.BlockCopy(entropy, 0, m_key, 0, keylen); m_iv = new byte[m_blocksizes[0] / 8]; Buffer.BlockCopy(entropy, entropy.Length - m_iv.Length - 1, m_iv, 0, m_iv.Length); m_bitSize = bitsize; }