예제 #1
0
        public static int GetVerificationCode(int value, string secretStr)
        {
            byte[] val = new byte[8];
            for (int i = 8; (i--) != 0; value >>= 8)
            {
                val[i] = (byte)(value & 0xFF);
            }

            byte[] secret = Base32Encoding.ToBytes(secretStr);
            if (secret == null || secret.Length == 0)
            {
                return(-1);
            }

            System.Security.Cryptography.HMACSHA1 hmac = new System.Security.Cryptography.HMACSHA1(secret, true);
            byte[] hash = hmac.ComputeHash(val);

            int  offset        = hash[SHA1_DIGEST_LENGTH - 1] & 0xF;
            uint truncatedHash = 0;

            for (int i = 0; i < 4; ++i)
            {
                truncatedHash <<= 8;
                truncatedHash  |= hash[offset + i];
            }
            truncatedHash &= 0x7FFFFFFF;
            truncatedHash %= 1000000;
            return((int)truncatedHash);
        }
예제 #2
0
        public static string CreateNewSecret(int secretLengthInBits)
        {
            byte[] buf = new byte[secretLengthInBits / 8];

            System.Security.Cryptography.RNGCryptoServiceProvider rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rnd.GetBytes(buf);

            return(Base32Encoding.ToString(buf));
        }