コード例 #1
0
        public static short FindAlgMaxLenght(OtpAlgorithmEnum alg, OtpValueTypeEnum valueType)
        {
            short length = (short)0;

            switch (alg)
            {
            case OtpAlgorithmEnum.Md5:
                length = (short)16;
                break;

            case OtpAlgorithmEnum.Sha1:
                length = (short)20;
                break;

            case OtpAlgorithmEnum.Des:
                length = (short)8;
                break;
            }
            switch (valueType)
            {
            case OtpValueTypeEnum.Raw:
                break;

            case OtpValueTypeEnum.HexaDecimal:
                length *= (short)2;
                break;

            case OtpValueTypeEnum.Numerical:
                length *= (short)3;
                break;
            }
            return(length);
        }
コード例 #2
0
        public static byte[] Signe(string appKey, string userKey, string data, OtpAlgorithmEnum AuthAlgorithm)
        {
            byte[] uKey  = Encoding.UTF8.GetBytes(userKey);
            byte[] aKey  = Encoding.UTF8.GetBytes(appKey);
            byte[] chall = Encoding.UTF8.GetBytes(data);

            byte[] inBuf = new byte[aKey.Length + uKey.Length + chall.Length];

            int j = 0;

            for (int i = 0; i < uKey.Length; i++)
            {
                inBuf[j++] = uKey[i];
            }

            for (int i = 0; i < aKey.Length; i++)
            {
                inBuf[j++] = aKey[i];
            }

            for (int i = 0; i < chall.Length; i++)
            {
                inBuf[j++] = chall[i];
            }

            byte[] hashedvalue = null;
            switch (AuthAlgorithm)
            {
            case OtpAlgorithmEnum.Md5:
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                hashedvalue = md5.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Sha1:
                System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
                hashedvalue = sha1.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Des:
                break;
            }
            return(hashedvalue);
        }
コード例 #3
0
        public static byte[] GenerateOtp(string appKey, string userKey, string challenge, OtpAlgorithmEnum AuthAlgorithm)
        {
            byte[] uKey  = Encoding.UTF8.GetBytes(userKey);
            byte[] aKey  = Encoding.UTF8.GetBytes(appKey);
            byte[] chall = Encoding.UTF8.GetBytes(challenge);

            byte[] inBuf = new byte[uKey.Length + aKey.Length + chall.Length];

            int j = 0;

            for (int i = 0; i < uKey.Length; i++)
            {
                inBuf[j++] = uKey[i];
            }

            for (int i = 0; i < aKey.Length; i++)
            {
                inBuf[j++] = aKey[i];
            }

            for (int i = 0; i < chall.Length; i++)
            {
                inBuf[j++] = chall[i];
            }

            byte[] hashedvalue = null;
            switch (AuthAlgorithm)
            {
            case OtpAlgorithmEnum.Md5:
                System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
                hashedvalue = md5.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Sha1:
                System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
                hashedvalue = sha1.ComputeHash(inBuf);
                break;

            case OtpAlgorithmEnum.Des:
                int lenAdj = inBuf.Length % 8;
                if (lenAdj != 0)
                {
                    lenAdj = 8 - lenAdj;
                }
                //int byteLen =
                byte[] temp = new byte[inBuf.Length + lenAdj];
                Array.Copy(inBuf, temp, inBuf.Length);
                for (int i = 0; i < lenAdj; i++)
                {
                    temp[inBuf.Length + i] = 0x00;
                }

                hashedvalue = Encrypt3DES(new MemoryStream(temp), uKey, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }).ToArray();
                break;
            }
            return(hashedvalue);
        }