internal static byte[] GenerateSteamParentalHash(byte[] password, byte[] salt, byte hashLength, ESteamParentalAlgorithm steamParentalAlgorithm)
        {
            if ((password == null) || (salt == null) || (hashLength == 0) || !Enum.IsDefined(typeof(ESteamParentalAlgorithm), steamParentalAlgorithm))
            {
                ASF.ArchiLogger.LogNullError(nameof(password) + " || " + nameof(salt) + " || " + nameof(hashLength) + " || " + nameof(steamParentalAlgorithm));

                return(null);
            }

            switch (steamParentalAlgorithm)
            {
            case ESteamParentalAlgorithm.Pbkdf2:
                using (HMACSHA1 hmacAlgorithm = new HMACSHA1(password)) {
                    return(Pbkdf2.ComputeDerivedKey(hmacAlgorithm, salt, SteamParentalPbkdf2Iterations, hashLength));
                }

            case ESteamParentalAlgorithm.SCrypt:
                return(SCrypt.ComputeDerivedKey(password, salt, SteamParentalSCryptIterations, SteamParentalSCryptBlocksCount, 1, null, hashLength));

            default:
                ASF.ArchiLogger.LogGenericError(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(steamParentalAlgorithm), steamParentalAlgorithm));

                return(null);
            }
        }
Beispiel #2
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            byte[] salt  = Concat(Encoding.UTF8.GetBytes("mnemonic"), Normalize(passphrase));
            byte[] bytes = Normalize(this._Mnemonic);

            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha512Digest());

            mac.Init(new KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
        }
Beispiel #3
0
        public static byte[] GetSeed(string words, string passphrase = "")
        {
            if (string.IsNullOrWhiteSpace(words))
            {
                throw new ArgumentNullException(nameof(words));
            }

            var pass = Utf8.Parse(words);
            var salt = Utf8.Parse("mnemonic" + passphrase);

            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(pass), salt, 2048, 64));
        }
Beispiel #4
0
        }         // HashPassword

        private HashedPassword Generate(string userName, string rawPassword, int?cycleCount, byte[] salt)
        {
            byte[] saltToUse = salt ?? GenerateSalt(userName);

            int iterations = cycleCount ?? this.minCycleCount;

            this.algo.Prepare(userName, rawPassword);

            byte[] hashedPassword = Pbkdf2.ComputeDerivedKey(this.algo.Instance, saltToUse, iterations, this.algo.HashSize);

            return(new HashedPassword(userName, iterations, hashedPassword, saltToUse));
        }         // Generate
Beispiel #5
0
        public void Return_Correct_SHA1_Derived_Key_With_Length_16_And_4096_Iterations()
        {
            var expectedResult = "56FA6AA75548099DCC37D7F03425E0C3";

            byte[] keyBytes     = Encoding.ASCII.GetBytes("pass\0word");
            byte[] saltBytes    = Encoding.ASCII.GetBytes("sa\0lt");
            byte[] derivedBytes = Pbkdf2.ComputeDerivedKey(hmacAlgorithm: new HMACSHA1(keyBytes),
                                                           salt: saltBytes, iterations: 4096, derivedKeyLength: 16);
            var result = Base16Encoding.Hex.GetString(derivedBytes);

            result.Should().Be(expectedResult);
        }
Beispiel #6
0
        public void Return_Correct_SHA1_Derived_Key_With_Length_25_And_4096_Iterations()
        {
            var expectedResult = "3D2EEC4FE41C849B80C8D83662C0E44A8B291A964CF2F07038";

            byte[] keyBytes     = Encoding.ASCII.GetBytes("passwordPASSWORDpassword");
            byte[] saltBytes    = Encoding.ASCII.GetBytes("saltSALTsaltSALTsaltSALTsaltSALTsalt");
            byte[] derivedBytes = Pbkdf2.ComputeDerivedKey(hmacAlgorithm: new HMACSHA1(keyBytes),
                                                           salt: saltBytes, iterations: 4096, derivedKeyLength: 25);
            var result = Base16Encoding.Hex.GetString(derivedBytes);

            result.Should().Be(expectedResult);
        }
Beispiel #7
0
        public void Return_Correct_SHA1_Derived_Key_With_Length_20_And_4096_Iterations()
        {
            var expectedResult = "4B007901B765489ABEAD49D926F721D065A429C1";

            byte[] keyBytes     = Encoding.ASCII.GetBytes("password");
            byte[] saltBytes    = Encoding.ASCII.GetBytes("salt");
            byte[] derivedBytes = Pbkdf2.ComputeDerivedKey(hmacAlgorithm: new HMACSHA1(keyBytes),
                                                           salt: saltBytes, iterations: 4096, derivedKeyLength: 20);
            var result = Base16Encoding.Hex.GetString(derivedBytes);

            result.Should().Be(expectedResult);
        }
Beispiel #8
0
        public void Return_Correct_SHA1_Derived_Key_With_Length_20_And_2_Iterations()
        {
            var expectedResult = "EA6C014DC72D6F8CCD1ED92ACE1D41F0D8DE8957";

            byte[] keyBytes     = Encoding.ASCII.GetBytes("password");
            byte[] saltBytes    = Encoding.ASCII.GetBytes("salt");
            byte[] derivedBytes = Pbkdf2.ComputeDerivedKey(hmacAlgorithm: new HMACSHA1(keyBytes),
                                                           salt: saltBytes, iterations: 2, derivedKeyLength: 20);
            var result = Base16Encoding.Hex.GetString(derivedBytes);

            result.Should().Be(expectedResult);
        }
Beispiel #9
0
        public static byte[] HashWithPbkdf2 <T>(string password, byte[] saltBytes, int iterations) where T : KeyedHashAlgorithm, new()
        {
            var passwordBytes = Encoding.UTF8.GetBytes(password);
            var algorithm     = new T()
            {
                Key = passwordBytes
            };
            var hashSize       = algorithm.HashSize / 8;
            var hashedPassword = Pbkdf2.ComputeDerivedKey(algorithm, saltBytes, iterations, hashSize);

            return(hashedPassword);
        }
Beispiel #10
0
        public void Return_Correct_SHA1_Derived_Key_With_Length_20_And_1_Iteration()
        {
            var expectedResult = "0C60C80F961F0E71F3A9B524AF6012062FE037A6";


            byte[] keyBytes     = Encoding.ASCII.GetBytes("password");
            byte[] saltBytes    = Encoding.ASCII.GetBytes("salt");
            byte[] derivedBytes = Pbkdf2.ComputeDerivedKey(hmacAlgorithm: new HMACSHA1(keyBytes),
                                                           salt: saltBytes, iterations: 1, derivedKeyLength: 20);
            var result = Base16Encoding.Hex.GetString(derivedBytes);

            result.Should().Be(expectedResult);
        }
Beispiel #11
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(NoBOMUTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(_Mnemonic);

#if USEBC || WINDOWS_UWP || NETSTANDARD1X
            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha512Digest());
            mac.Init(new KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#else
            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), salt, 2048, 64));
#endif
        }
Beispiel #12
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(Encoding.UTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(_Mnemonic);

#if USEBC || WINDOWS_UWP || NETCORE
            var mac = new nStratis.BouncyCastle.crypto.macs.HMac(new nStratis.BouncyCastle.crypto.digests.Sha512Digest());
            mac.Init(new nStratis.BouncyCastle.crypto.parameters.KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#else
            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), salt, 2048, 64));
#endif
        }
Beispiel #13
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(Encoding.UTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(_Mnemonic);

#if !USEBC
            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), salt, 2048, 64));
#else
            var mac = MacUtilities.GetMac("HMAC-SHA_512");
            mac.Init(new KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#endif
        }
Beispiel #14
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(Encoding.UTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(this._Mnemonic);

#if NETCORE
            var mac = new HMac(new Sha512Digest());
            mac.Init(new KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#else
            return(Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), salt, 2048, 64));
#endif
        }
Beispiel #15
0
        private Key GetKeyFromPassword(string password)
        {
            var bytes = Encoding.UTF8.GetBytes(password);

#pragma warning disable CS0618 // Type or member is obsolete
#if USEBC || WINDOWS_UWP || NETSTANDARD1X
            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha512Digest());
            mac.Init(new NBitcoin.BouncyCastle.Crypto.Parameters.KeyParameter(bytes));
            var secret = Pbkdf2.ComputeDerivedKey(mac, new byte[0], 1024, 32);
#else
            var secret = Pbkdf2.ComputeDerivedKey(new HMACSHA512(bytes), new byte[0], 1024, 32);
#endif
#pragma warning restore CS0618
            return(new Key(secret));
        }
Beispiel #16
0
        public byte[] DeriveSeed(string passphrase = null)
        {
            passphrase = passphrase ?? "";
            var salt  = Concat(NoBOMUTF8.GetBytes("mnemonic"), Normalize(passphrase));
            var bytes = Normalize(_Mnemonic);

#if NO_NATIVE_HMACSHA512
            var mac = new NBitcoin.BouncyCastle.Crypto.Macs.HMac(new NBitcoin.BouncyCastle.Crypto.Digests.Sha512Digest());
            mac.Init(new NBitcoin.BouncyCastle.Crypto.Parameters.KeyParameter(bytes));
            return(Pbkdf2.ComputeDerivedKey(mac, salt, 2048, 64));
#elif NO_NATIVE_RFC2898_HMACSHA512
            return(NBitcoin.Crypto.Pbkdf2.ComputeDerivedKey(new System.Security.Cryptography.HMACSHA512(bytes), salt, 2048, 64));
#else
            using System.Security.Cryptography.Rfc2898DeriveBytes derive = new System.Security.Cryptography.Rfc2898DeriveBytes(bytes, salt, 2048, System.Security.Cryptography.HashAlgorithmName.SHA512);
            return(derive.GetBytes(64));
#endif
        }
Beispiel #17
0
        static void TestFile(string filename)
        {
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(filename))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    int startTime = Environment.TickCount;

                    string line; int count = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        Console.Write(".");

                        string[] parts = line.Split(new[] { ',' }, 4);
                        if (parts.Length != 4)
                        {
                            continue;
                        }

                        byte[] key         = Convert.FromBase64String(parts[0]);
                        byte[] salt        = Convert.FromBase64String(parts[1]);
                        int    iterations  = int.Parse(parts[2]);
                        byte[] expectedKey = Convert.FromBase64String(parts[3]);
                        byte[] derivedKey  = Pbkdf2.ComputeDerivedKey(new HMACSHA256(key), salt, iterations, 128);

                        for (int i = 0; i < expectedKey.Length; i++)
                        {
                            if (expectedKey[i] != derivedKey[i])
                            {
                                Console.WriteLine("PBKDF2 entry #{0} does not match.", count + 1);
                                break;
                            }
                        }

                        count++;
                    }

                    Console.Write("...({0} ms for {1} vectors)...", Environment.TickCount - startTime, count);
                }
            }
        }
Beispiel #18
0
        public void Return_Expected_Results_From_TestVectors_File()
        {
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CryptSharpStandard.SCryptSubset.UnitTests.TestVectors-PBKDF2.txt"))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    int startTime = Environment.TickCount;

                    string line; int count = 0;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] parts = line.Split(new[] { ',' }, 4);
                        if (parts.Length != 4)
                        {
                            continue;
                        }

                        byte[] key         = Convert.FromBase64String(parts[0]);
                        byte[] salt        = Convert.FromBase64String(parts[1]);
                        int    iterations  = int.Parse(parts[2]);
                        byte[] expectedKey = Convert.FromBase64String(parts[3]);
                        byte[] derivedKey  = Pbkdf2.ComputeDerivedKey(new HMACSHA256(key), salt, iterations, 128);

                        for (int i = 0; i < expectedKey.Length; i++)
                        {
                            if (expectedKey[i] != derivedKey[i])
                            {
                                derivedKey[i].Should().Be(expectedKey[i], "PBKDF2 entry #{0} does not match.", count + 1);
                                break;
                            }
                        }
                        count++;
                    }
                }
            }
        }
Beispiel #19
0
        public static byte[] DeriveKeyWithConfig(byte[] key, byte[] salt, int outputSize, Pbkdf2Configuration config)
        {
            var hmac = AuthenticatorFactory.CreateHmacPrimitive(config.FunctionName.ToEnum <HashFunction>(), key, null);

            return(Pbkdf2.ComputeDerivedKey(hmac, salt, config.Iterations, outputSize));
        }
Beispiel #20
0
        private static byte[] DeriveKey(byte[] key, byte[] salt, int outputSize, int iterations)
        {
            var hmac = AuthenticatorFactory.CreateHmacPrimitive(DefaultFunction, key, null);

            return(Pbkdf2.ComputeDerivedKey(hmac, salt, iterations, outputSize));
        }
 /// <summary>
 /// Create Password Object from database
 /// </summary>
 /// <param name="passwordBytes"></param>
 /// <param name="saltBytes"></param>
 public Password(byte[] passwordBytes, byte[] saltBytes)
 {
     this.password   = passwordBytes;
     this.Salt       = saltBytes;
     this.DerivedKey = Pbkdf2.ComputeDerivedKey(new HMACSHA256(password), Salt, iterations, keyLength);
 }