• 大小: 9.67KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-05-03
  • 语言: C#
  • 标签: google  go  

资源简介

C#使用 谷歌身份认证器,动态口令,GoogleAuthenticator 基于时间的一次性密码

资源截图

代码片段和文件信息

using System;
namespace GoogleAuthorization
{
    public static class base32
    {
        public static byte[] ToBytes(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                throw new ArgumentNullException(“input“);
            }

            input = input.TrimEnd(‘=‘); 
            int byteCount = input.Length * 5 / 8; 
            byte[] returnArray = new byte[byteCount];

            byte curByte = 0 bitsRemaining = 8;
            int mask = 0 arrayIndex = 0;

            foreach (char c in input)
            {
                int cValue = CharToValue(c);

                if (bitsRemaining > 5)
                {
                    mask = cValue << (bitsRemaining - 5);
                    curByte = (byte)(curByte | mask);
                    bitsRemaining -= 5;
                }
                else
                {
                    mask = cValue >> (5 - bitsRemaining);
                    curByte = (byte)(curByte | mask);
                    returnArray[arrayIndex++] = curByte;
                    curByte = (byte)(cValue << (3 + bitsRemaining));
                    bitsRemaining += 3;
                }
            }

            if (arrayIndex != byteCount)
            {
                returnArray[arrayIndex] = curByte;
            }

            return returnArray;
        }

        public static string ToString(byte[] input)
        {
            if (input == null || input.Length == 0)
            {
                throw new ArgumentNullException(“input“);
            }

            int charCount = (int)Math.Ceiling(input.Length / 5d) * 8;
            char[] returnArray = new char[charCount];

            byte nextChar = 0 bitsRemaining = 5;
            int arrayIndex = 0;

            foreach (byte b in input)
            {
                nextChar = (byte)(nextChar | (b >> (8 - bitsRemaining)));
                returnArray[arrayIndex++] = ValueToChar(nextChar);

                if (bitsRemaining < 4)
                {
                    nextChar = (byte)((b >> (3 - bitsRemaining)) & 31);
                    returnArray[arrayIndex++] = ValueToChar(nextChar);
                    bitsRemaining += 5;
                }

                bitsRemaining -= 3;
                nextChar = (byte)((b << bitsRemaining) & 31);
            }

            if (arrayIndex != charCount)
            {
                returnArray[arrayIndex++] = ValueToChar(nextChar);
                while (arrayIndex != charCount) returnArray[arrayIndex++] = ‘=‘; 
            }

            return new string(returnArray);
        }

        private static int CharToValue(char c)
        {
            var value = (int)c;

            if (value < 91 && value > 64)
            {
                return value - 65;
            }
            if (value < 56 && value > 49)
            {
                return value - 24;
            }
            if (value < 123 && value > 96)
            {
                return value - 97;
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-11-13 01:22  GoogleAuthenticator-master\
     文件        5850  2019-11-13 01:22  GoogleAuthenticator-master\.gitignore
     文件        1004  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthenticator.sln
     目录           0  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\
     文件        3453  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\base32.cs
     文件        3420  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\GoogleAuthenticator.cs
     文件        2545  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\GoogleAuthenticator.csproj
     文件         873  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\Program.cs
     目录           0  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\Properties\
     文件        1326  2019-11-13 01:22  GoogleAuthenticator-master\GoogleAuthorization\Properties\AssemblyInfo.cs
     文件        1028  2019-11-13 01:22  GoogleAuthenticator-master\README.md

评论

共有 条评论