コード例 #1
0
 public static StoredPassword From(byte[] hash, byte[] salt, TargetHashConfigration opts)
 {
     return(new StoredPassword
     {
         Hash = hash,
         Salt = salt,
         Options = opts,
     });
 }
コード例 #2
0
 public Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options)
 {
     return(Task.Factory.StartNew(() =>
                                  KeyDerivation.Pbkdf2(
                                      password: BitConverter.ToString(password),
                                      salt: salt,
                                      prf: Prf(options),
                                      iterationCount: options.IterationCount ?? throw new InvalidOperationException($"{nameof(options.IterationCount)} required for PBKDF2"),
                                      numBytesRequested: options.PasswordHashBytes ?? throw new InvalidOperationException($"{nameof(options.PasswordHashBytes)} required for PBKDF2"))
                                  ));
 }
コード例 #3
0
        public Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options)
        {
            switch (options.Algorithm ?? throw new InvalidOperationException($"{nameof(options.Algorithm)} required for hashing passwords"))
            {
            case HashingAlgorithm.SHA1:
                return(sha1.Hash(password, salt, options));

            case HashingAlgorithm.PBKDF2:
                return(pbkdf2.Hash(password, salt, options));

            default:
                throw new NotImplementedException($"Hashing algorithm {options.Algorithm} not yet implemented");
            }
        }
コード例 #4
0
        public async Task <byte[]> Hash(byte[] password, byte[] salt, TargetHashConfigration options)
        {
            using (var stream = new MemoryStream(password.Length + salt.Length))
            {
                await stream.WriteAsync(password, 0, password.Length);

                await stream.WriteAsync(salt, 0, salt.Length);

                stream.Seek(0, SeekOrigin.Begin);

                using (var sha = new SHA1CryptoServiceProvider())
                {
                    return(sha.ComputeHash(stream));
                }
            }
        }
コード例 #5
0
        private KeyDerivationPrf Prf(TargetHashConfigration options)
        {
            switch (options.Algorithm2 ?? throw new InvalidOperationException($"{nameof(options.IterationCount)} required for PBKDF2"))
            {
            case HashingAlgorithm.SHA1:
                return(KeyDerivationPrf.HMACSHA1);

            case HashingAlgorithm.SHA256:
                return(KeyDerivationPrf.HMACSHA256);

            case HashingAlgorithm.SHA512:
                return(KeyDerivationPrf.HMACSHA512);

            default:
                throw new IndexOutOfRangeException($"{options.Algorithm2.Value} is unsupported Algorithm2 for PBKDF2");
            }
        }