예제 #1
0
        static byte[] ComputeHash(Algo algo, string passwordStr)
        {
            var salt1         = algo.Salt1.ToArrayUnsafe();
            var salt2         = algo.Salt2.ToArrayUnsafe();
            var passwordBytes = Encoding.UTF8.GetBytes(passwordStr);

            var hash1 = Sha256(salt1, passwordBytes, salt1);
            var hash2 = Sha256(salt2, hash1, salt2);
            var hash3 = Pbkdf2Sha512(64, hash2, salt1, 100000);

            return(Sha256(salt2, hash3, salt2));
        }
예제 #2
0
        public static CheckPassword GenRequest(Password pwdInfo, Algo algo, string passwordStr)
        {
            var hash = ComputeHash(algo, passwordStr);

            var pBytes    = algo.P.ToArrayUnsafe().Apply(WithHashPadding);
            var p         = UnsignedNum(pBytes);
            var g         = new BigInteger(algo.G);
            var bigBBytes = pwdInfo.SrpB.Map(bts => bts.ToArrayUnsafe()).IfNone(() => new byte[0]).Apply(WithHashPadding);
            var bigB      = UnsignedNum(bigBBytes);

            /*
             * if (!MTP::IsPrimeAndGood(algo.p, algo.g)) {
             *          LOG(("API Error: Bad p/g in cloud password creation!"));
             *      return failed();
             *  } else if (!IsGoodLarge(B, p)) {
             *          LOG(("API Error: Bad B in cloud password check!"));
             *          return failed();
             *  }
             */

            var x      = UnsignedNum(hash);
            var gBytes = ToBytes(g);
            var gX     = g.ModPow(x, p);
            var k      = UnsignedNum(Sha256(pBytes, gBytes));
            var kgX    = (k * gX).Remainder(p);

            var(a, bigABytes, u) = GenerateAndCheckRandom(g, bigBBytes, p);
            var gB = (bigB - kgX).Remainder(p);

            Helpers.Assert(IsGoodModExpFirst(gB, p), "Bad g_b in cloud password check!");

            var ux    = u * x;
            var aUx   = a + ux;
            var bigS  = gB.ModPow(aUx, p);
            var bigK  = Sha256(ToBytes(bigS));
            var bigM1 = Sha256(
                Xor(Sha256(pBytes), Sha256(gBytes)),
                Sha256(algo.Salt1.ToArrayUnsafe()),
                Sha256(algo.Salt2.ToArrayUnsafe()),
                bigABytes,
                bigBBytes,
                bigK
                );

            return(new CheckPassword(password: new InputCheckPasswordSrp.Tag(
                                         srpId: pwdInfo.SrpId.IfNone(0),
                                         a: bigABytes.ToBytesUnsafe(),
                                         m1: bigM1.ToBytesUnsafe()
                                         )));
        }
예제 #3
0
        static byte[] ComputeHash(Algo algo, SecureString passwordStr)
        {
            var salt1        = algo.Salt1.ToArrayUnsafe();
            var salt1Segment = new ArraySegment <byte>(salt1);
            var salt2        = algo.Salt2.ToArrayUnsafe();

            var hash1 = UseSecureStringUtf8Representation(
                passwordStr,
                pwd => Sha256(salt1Segment, pwd, salt1Segment)
                );
            var hash2 = Sha256(salt2, hash1, salt2);
            var hash3 = Pbkdf2Sha512(64, hash2, salt1, 100000);

            return(Sha256(salt2, hash3, salt2));
        }