Exemplo n.º 1
0
        public static ExtraHashAlgorithm CreateMD4()
        {
            var ha = new MD4Digest();

            return(new ExtraHashAlgorithm(
                       ha.AlgorithmName, ha.GetDigestSize(),
                       ha.Reset, ha.BlockUpdate, ha.DoFinal
                       ));
        }
Exemplo n.º 2
0
        public static string CalcNTLM(string password)
        {
            MD4Digest digest = new MD4Digest();

            byte[] bytes = Encoding.Unicode.GetBytes(password);
            digest.BlockUpdate(bytes, 0, bytes.Length);
            byte[] NTLMOut = new byte[16];
            digest.DoFinal(NTLMOut, 0);

            return(ToHexString(NTLMOut));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Convert Password to NT Hash.  Convert to unicode and MD4
        /// </summary>
        /// <param name="passwordIn">password In</param>
        /// <returns>NT Hash as byte[]</returns>
        public static byte[] NTHashAsBytes(string passwordIn)
        {
            MD4Digest md = new MD4Digest();

            byte[] unicodePassword = Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(passwordIn));


            md.BlockUpdate(unicodePassword, 0, unicodePassword.Length);
            byte[] hash = new byte[16];
            md.DoFinal(hash, 0);


            return(hash);
        }
Exemplo n.º 4
0
        public override string hash(string pw)
        {
            MD4Digest digest = new MD4Digest();

            // We want Unicode here (UTF-16)
            byte[] password = Encoding.Unicode.GetBytes(pw);

            digest.BlockUpdate(password, 0, password.Length);
            int digestSize = digest.GetDigestSize();

            byte[] hashData = new byte[digestSize];
            digest.DoFinal(hashData, 0);

            // Convert to HEX and return
            return(string.Concat(hashData.Select(b => b.ToString("X2"))));
        }
Exemplo n.º 5
0
 /// <summary>
 /// MD4 computation
 /// </summary>
 /// <param name="data">the data to hash</param>
 /// <param name="key">optional hmac key</param>
 /// <returns>the hash value</returns>
 private static byte[] ComputeMd4(byte[] data, byte[] key)
 {
     byte[] outData = new byte[16];
     if (key != null)
     {
         var digest = new HMac(new MD4Digest());
         digest.Init(new KeyParameter(key));
         digest.BlockUpdate(data, 0, data.Length);
         digest.DoFinal(outData, 0);
     }
     else
     {
         var digest = new MD4Digest();
         digest.BlockUpdate(data, 0, data.Length);
         digest.DoFinal(outData, 0);
     }
     return(outData);
 }
Exemplo n.º 6
0
        public ITestResult Perform()
        {
            IDigest digest = new MD4Digest();

            byte[] resBuf = new byte[digest.GetDigestSize()];
            string resStr;

            //
            // test 1
            //
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec1.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 1"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec1
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            //
            // test 2
            //
            byte[] bytes = Hex.Decode(testVec2);

            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec2.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 2"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec2
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            //
            // test 3
            //
            bytes = Hex.Decode(testVec3);

            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec3.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 3"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec3
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            //
            // test 4
            //
            bytes = Hex.Decode(testVec4);

            digest.BlockUpdate(bytes, 0, bytes.Length);

            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 4"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec4
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            //
            // test 5
            //
            bytes = Hex.Decode(testVec4);

            digest.BlockUpdate(bytes, 0, bytes.Length / 2);

            // clone the IDigest
            IDigest d = new MD4Digest((MD4Digest)digest);

            digest.BlockUpdate(bytes, bytes.Length / 2, bytes.Length - bytes.Length / 2);
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 5"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec4
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            d.BlockUpdate(bytes, bytes.Length / 2, bytes.Length - bytes.Length / 2);
            d.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec4.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "MD4 failing standard vector test 5"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec4
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }
            return(new SimpleTestResult(true, Name + ": Okay"));
        }
Exemplo n.º 7
0
        public static IDigest GetDigest(string algorithm)
        {
            string text  = Platform.ToUpperInvariant(algorithm);
            string text2 = (string)DigestUtilities.algorithms[text];

            if (text2 == null)
            {
                text2 = text;
            }
            try
            {
                switch ((DigestUtilities.DigestAlgorithm)Enums.GetEnumValue(typeof(DigestUtilities.DigestAlgorithm), text2))
                {
                case DigestUtilities.DigestAlgorithm.GOST3411:
                {
                    IDigest result = new Gost3411Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.MD2:
                {
                    IDigest result = new MD2Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.MD4:
                {
                    IDigest result = new MD4Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.MD5:
                {
                    IDigest result = new MD5Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.RIPEMD128:
                {
                    IDigest result = new RipeMD128Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.RIPEMD160:
                {
                    IDigest result = new RipeMD160Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.RIPEMD256:
                {
                    IDigest result = new RipeMD256Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.RIPEMD320:
                {
                    IDigest result = new RipeMD320Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_1:
                {
                    IDigest result = new Sha1Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_224:
                {
                    IDigest result = new Sha224Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_256:
                {
                    IDigest result = new Sha256Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_384:
                {
                    IDigest result = new Sha384Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_512:
                {
                    IDigest result = new Sha512Digest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_512_224:
                {
                    IDigest result = new Sha512tDigest(224);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA_512_256:
                {
                    IDigest result = new Sha512tDigest(256);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA3_224:
                {
                    IDigest result = new Sha3Digest(224);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA3_256:
                {
                    IDigest result = new Sha3Digest(256);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA3_384:
                {
                    IDigest result = new Sha3Digest(384);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.SHA3_512:
                {
                    IDigest result = new Sha3Digest(512);
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.TIGER:
                {
                    IDigest result = new TigerDigest();
                    return(result);
                }

                case DigestUtilities.DigestAlgorithm.WHIRLPOOL:
                {
                    IDigest result = new WhirlpoolDigest();
                    return(result);
                }
                }
            }
            catch (ArgumentException)
            {
            }
            throw new SecurityUtilityException("Digest " + text2 + " not recognised.");
        }