private void BlockMix(ArraySegment <byte> B, byte[] Y) { ArraySegment <byte> .Copy(B, (2 * BlockSize - 1) * 64, BlockX, 0, 64); for (int i = 0; i < 2 * BlockSize; ++i) { for (int j = 0; j < 64; ++j) { BlockX[j] ^= B[i * 64 + j]; } Salsa.ComputeHash(BlockX, BlockX); Array.Copy(BlockX, 0, Y, i * 64, 64); } for (int i = 0; i < BlockSize; ++i) { ArraySegment <byte> .Copy(Y, (i * 2) * 64, B, i * 64, 64); } for (int i = 0; i < BlockSize; ++i) { ArraySegment <byte> .Copy(Y, (i * 2 + 1) * 64, B, (i + BlockSize) * 64, 64); } }
public override byte[] DeriveBytes(int count) { if (count > 64) { throw new ArgumentException("Salsa20 can only derive up to 64 bytes.", "count"); } byte[] hash; var block = new byte[64]; if (Key.Length == 16) { block[0] = 101; block[1] = 120; block[2] = 112; block[3] = 97; Array.Copy(Key, 0, block, 4, 16); block[20] = 110; block[21] = 100; block[22] = 32; block[23] = 49; Array.Copy(Nonce, 0, block, 24, 16); block[40] = 54; block[41] = 45; block[42] = 98; block[43] = 121; Array.Copy(Key, 0, block, 44, 16); block[60] = 116; block[61] = 101; block[62] = 32; block[63] = 107; hash = Salsa.ComputeHash(block); } else { block[0] = 101; block[1] = 120; block[2] = 112; block[3] = 97; Array.Copy(Key, 0, block, 4, 16); block[20] = 110; block[21] = 100; block[22] = 32; block[23] = 51; Array.Copy(Nonce, 0, block, 24, 16); block[40] = 50; block[41] = 45; block[42] = 98; block[43] = 121; Array.Copy(Key, 16, block, 44, 16); block[60] = 116; block[61] = 101; block[62] = 32; block[63] = 107; hash = Salsa.ComputeHash(block); } var result = new byte[count]; Array.Copy(hash, result, count); return(result); }