示例#1
0
        public byte[] HashPassword(int amountBytesToReturn, byte[] passwordBytes, int iterations, int memorySize, byte[] salt = null,
                                   byte[] associatedData = null, byte[] knownSecret = null, int degreeOfParallelism = 0)
        {
            salt = salt ?? EncryptionUtils.GenerateRandomBytes(16); // generate a 128 bits salt, if not provided
            degreeOfParallelism = (degreeOfParallelism == 0 ? Environment.ProcessorCount : degreeOfParallelism);

            var argon2id = new Konscious.Security.Cryptography.Argon2id(passwordBytes)
            {
                Salt = salt,
                DegreeOfParallelism = degreeOfParallelism,
                Iterations          = iterations,
                MemorySize          = memorySize,
                AssociatedData      = associatedData,
                KnownSecret         = knownSecret
            };

            return(argon2id.GetBytes(amountBytesToReturn));
        }
示例#2
0
        public Argon2idHashResult ComputeHash(byte[] stringToComputeHashBytes, int iterations, int kBmemorySize, int degreeOfParallelism, int amountBytesToReturn,
                                              byte[] salt = null, byte[] associatedData = null, byte[] knownSecret = null)
        {
            try
            {
                salt = salt ?? CommonMethods.GenerateSalt(); // generate a 128 bits salt, if not provided
                degreeOfParallelism = (degreeOfParallelism <= 0 ? Environment.ProcessorCount : degreeOfParallelism);

                using (var argon2id = new Konscious.Security.Cryptography.Argon2id(stringToComputeHashBytes)
                {
                    Salt = salt,
                    DegreeOfParallelism = degreeOfParallelism,
                    Iterations = iterations,
                    MemorySize = kBmemorySize,
                    AssociatedData = associatedData,
                    KnownSecret = knownSecret
                })
                {
                    var hashBytes = argon2id.GetBytes(amountBytesToReturn);

                    return(new Argon2idHashResult()
                    {
                        Success = true,
                        HashBytes = hashBytes,
                        HashString = Convert.ToBase64String(hashBytes),
                        Message = MessageDictionary.Instance["Hash.ComputeSuccess"],
                        SaltBytes = salt,
                        Iterations = iterations,
                        DegreeOfParallelism = degreeOfParallelism,
                        KBMemorySize = kBmemorySize,
                        AssociatedData = associatedData,
                        KnownSecret = knownSecret
                    });
                }
            }
            catch (Exception ex)
            {
                return(new Argon2idHashResult()
                {
                    Success = false,
                    Message = ex.ToString()
                });
            }
        }