예제 #1
0
파일: Program.cs 프로젝트: Arsslensoft/OCT
        static byte[] Sha224(byte[] dat)
        {
            Sha224Digest digester = new Sha224Digest();

            byte[] retValue = new byte[digester.GetDigestSize()];
            digester.BlockUpdate(dat, 0, dat.Length);
            digester.DoFinal(retValue, 0);
            return(retValue);
        }
예제 #2
0
        public static byte[] Hash224(byte[] bytes)
        {
            var digest = new Sha224Digest();
            var output = new byte[digest.GetDigestSize()];

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

            return(output);
        }
예제 #3
0
        private byte[] _PasswordToKey(byte[] password, byte[] engineId)
        {
            Sha224Digest sha = new Sha224Digest();
            {
                var passwordIndex = 0;
                var count         = 0;
                /* Use while loop until we've done 1 Megabyte */
                var sourceBuffer = new byte[1048576];
                var buf          = new byte[64];
                while (count < 1048576)
                {
                    for (var i = 0; i < 64; ++i)
                    {
                        // Take the next octet of the password, wrapping
                        // to the beginning of the password as necessary.
                        buf[i] = password[passwordIndex++ % password.Length];
                    }

                    Buffer.BlockCopy(buf, 0, sourceBuffer, count, buf.Length);
                    count += 64;
                }

                var digest = new byte[sha.GetDigestSize()];
                sha.BlockUpdate(sourceBuffer, 0, sourceBuffer.Length);
                sha.DoFinal(digest, 0);

                using (var buffer = new MemoryStream())
                {
                    buffer.Write(digest, 0, digest.Length);
                    buffer.Write(engineId, 0, engineId.Length);
                    buffer.Write(digest, 0, digest.Length);
                    var input = buffer.ToArray();
                    sha.BlockUpdate(input, 0, input.Length);
                    sha.DoFinal(digest, 0);
                    return(digest);
                }
            }
        }
예제 #4
0
        public ITestResult Perform()
        {
            IDigest digest = new Sha224Digest();

            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,
                                            "SHA-224 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,
                                            "SHA-224 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,
                                            "SHA-224 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,
                                            "SHA-224 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 Sha224Digest((Sha224Digest)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,
                                            "SHA-224 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,
                                            "SHA-224 failing standard vector test 5"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec4
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            // test 6
            bytes = Hex.Decode(testVec5);
            for (int i = 0; i < 100000; i++)
            {
                digest.BlockUpdate(bytes, 0, bytes.Length);
            }
            digest.DoFinal(resBuf, 0);

            resStr = Hex.ToHexString(resBuf);
            if (!resVec5.Equals(resStr))
            {
                return(new SimpleTestResult(false,
                                            "SHA-224 failing standard vector test 6"
                                            + SimpleTest.NewLine
                                            + "    expected: " + resVec5
                                            + SimpleTest.NewLine
                                            + "    got     : " + resStr));
            }

            return(new SimpleTestResult(true, Name + ": Okay"));
        }
예제 #5
0
 protected override byte[] HashFinal()
 {
     _digest.DoFinal(HashValue, 0);
     return(HashValue);
 }