Esempio n. 1
0
        private static void HMacSha256_Sample()
        {
            var s = "hello hmac sha256";

            Console.WriteLine(s);
            var k = HMACSHA256.GeneratorKey();

            Console.WriteLine("密钥(十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(k));
            Console.WriteLine("密钥(Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(k));
            Console.WriteLine();
            var b1 = HMACSHA256.Compute(s, k);

            Console.WriteLine("使用BouncyCastle计算结果(转Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(b1));
            Console.WriteLine("使用BouncyCastle计算结果(转十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(b1));
            Console.WriteLine();
            var b2 = HMACSHA256.Compute2(s, k);

            Console.WriteLine("不使用BouncyCastle计算结果(转Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(b2));
            Console.WriteLine("不使用BouncyCastle计算结果(转十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(b2));
        }
Esempio n. 2
0
        private static void HMacSha256_Sample()
        {
            var s = "hello hmac sha256";

            for (int i = 0; i < 50; i++)
            {
                var key = HMACSHA256.GeneratorKey();

                var salt = Str.GetRandomString(16);
                Console.WriteLine(salt);
                var pass = Str.GenerateRandom(16);
                Console.WriteLine(pass);
                var res  = HMACSHA256.Compute(salt + pass, key);
                var res2 = HMACSHA256.Compute2(salt + pass, key);
                Console.WriteLine(Base64.ToBase64String(res));
                Console.WriteLine(Base64.ToBase64String(res2));
            }


            Console.WriteLine(s);
            var k = HMACSHA256.GeneratorKey();

            Console.WriteLine("密钥(十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(k));
            Console.WriteLine("密钥(Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(k));
            Console.WriteLine();
            var b1 = HMACSHA256.Compute(s, k);

            Console.WriteLine("使用BouncyCastle计算结果(转Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(b1));
            Console.WriteLine("使用BouncyCastle计算结果(转十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(b1));
            Console.WriteLine();
            var b2 = HMACSHA256.Compute2(s, k);

            Console.WriteLine("不使用BouncyCastle计算结果(转Base64字符串):");
            Console.WriteLine(Base64.ToBase64String(b2));
            Console.WriteLine("不使用BouncyCastle计算结果(转十六进制字符串):");
            Console.WriteLine(Hex.ToHexString(b2));
        }