コード例 #1
0
        public byte[] HashPassword(string password, ISecureRandomGenerator secureRandomGenerator)
        {
            var passwordBytes = Encoding.UTF8.GetBytes(password);
            var salt          = secureRandomGenerator.GenerateBytes(32);
            var iv            = secureRandomGenerator.GenerateBytes(16);

            var cipher = Aes.Create();

            cipher.KeySize = 256;
            cipher.Padding = PaddingMode.PKCS7;
            cipher.Mode    = CipherMode.CBC;
            cipher.Key     = salt;
            cipher.IV      = iv;
            var encryptor = cipher.CreateEncryptor();
            var subKey    = encryptor.TransformFinalBlock(passwordBytes, 0, passwordBytes.Length);

            var outputBytes = new byte[9 + salt.Length + iv.Length + subKey.Length];

            outputBytes[0] = FormatMarkers.Aes256;
            BufferUtil.WriteNetworkByteOrder(outputBytes, 1, (uint)cipher.Padding);
            BufferUtil.WriteNetworkByteOrder(outputBytes, 5, (uint)cipher.Mode);
            BufferUtil.BlockFill(salt, outputBytes, 9);
            BufferUtil.BlockFill(iv, outputBytes, 9 + salt.Length);
            BufferUtil.BlockFill(subKey, outputBytes, 9 + salt.Length + iv.Length);
            return(outputBytes);
        }