Exemplo n.º 1
0
        public static byte[] Compress(byte[] buffer)
        {
            using (MemoryStream ms = new MemoryStream())
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    uint checkSum = 0;

                    using (Crc crc = new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)))
                    {
                        checkSum = BitConverter.ToUInt32(crc.ComputeHash(buffer), 0);
                    }

                    byte[] compressed = DeflateBuffer(buffer);

                    Int32 poslocal = WriteHeader(writer, LocalFileHeader);
                    WriteLocalFile(writer, "z", checkSum, ( UInt32 )buffer.Length, compressed);

                    Int32  posCDR  = WriteHeader(writer, CentralDirectoryHeader);
                    UInt32 CDRSize = WriteCentralDirectory(writer, "z", checkSum, ( UInt32 )compressed.Length, ( UInt32 )buffer.Length, poslocal);

                    Int32 posEOD = WriteHeader(writer, EndOfDirectoryHeader);
                    WriteEndOfDirectory(writer, 1, CDRSize, posCDR);

                    return(ms.ToArray());
                }
        }
        public void Constructor4Test()
        {
            HashChain chain = new HashChain(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), TestVectors.Battery.NumericRepeated, 0, 10, 5);

            for (int i = 0; i < NumericCrc32VectorChain.Length; i++)
            {
                CustomAssert.AreEqual(NumericCrc32VectorChain[i], chain[i]);
            }
        }
Exemplo n.º 3
0
        public static byte[] CRCHash(byte[] input)
        {
            using (Crc crc = new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)))
            {
                byte[] hash = crc.ComputeHash(input);
                Array.Reverse(hash);

                return(hash);
            }
        }
        public void Enumerator1Test()
        {
            HashChain chain = new HashChain(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), TestVectors.Battery.Numeric);

            int i = 0;

            foreach (byte[] hash in chain)
            {
                CustomAssert.AreEqual(NumericCrc32VectorChain[i], hash);
                i++;
            }
        }
        public void Enumerator3Test()
        {
            HashChain chain = new HashChain(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), TestVectors.Battery.Numeric);

            IEnumerator e = ((IEnumerable)chain).GetEnumerator();
            int         i = 0;

            while (e.MoveNext())
            {
                CustomAssert.AreEqual(NumericCrc32VectorChain[i], (byte[])e.Current);
                i++;
            }
        }
        public void Constructor2Test()
        {
            HashList list = new HashList(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), 10);

            CustomAssert.AreEqual(AlphanumericCrc32TopHash, list.ComputeHash(TestVectors.Battery.Alphanumeric));

            for (int i = 0; i < AlphanumericCrc32VectorList.Length; i++)
            {
                CustomAssert.AreEqual(AlphanumericCrc32VectorList[i], list[i].Hash);
            }

            Assert.AreEqual(10, list.BlockSize);
        }
        public void Enumerator1Test()
        {
            HashList list = new HashList(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), 10);

            list.ComputeHash(TestVectors.Battery.Alphanumeric);

            int i = 0;

            foreach (HashNode node in list)
            {
                CustomAssert.AreEqual(AlphanumericCrc32VectorList[i], node.Hash);
                i++;
            }
        }
        public void Enumerator3Test()
        {
            HashList list = new HashList(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), 10);

            list.ComputeHash(TestVectors.Battery.Alphanumeric);

            IEnumerator e = ((IEnumerable)list).GetEnumerator();
            int         i = 0;

            while (e.MoveNext())
            {
                CustomAssert.AreEqual(AlphanumericCrc32VectorList[i], ((HashNode)e.Current).Hash);
                i++;
            }
        }
Exemplo n.º 9
0
        private IList <CrcParameters> LoadParameters(string path)
        {
            var parameters = new List <CrcParameters>();
            var lines      = File.ReadAllLines(path);
            var regex      = new Regex(@"width=([^ ]+) poly=([^ ]+) init=([^ ]+) refin=([^ ]+) refout=([^ ]+) xorout=([^ ]+) check=([^ ]+) residue=([^ ]+) name=""(.+)""", RegexOptions.Compiled);

            foreach (var line in lines)
            {
                var correctedLine = line.Replace("0x", "");
                var match         = regex.Match(correctedLine);
                var size          = byte.Parse(match.Groups[1].Value);
                var poly          = ulong.Parse(match.Groups[2].Value, NumberStyles.HexNumber);
                var init          = ulong.Parse(match.Groups[3].Value, NumberStyles.HexNumber);
                var refin         = bool.Parse(match.Groups[4].Value);
                var refout        = bool.Parse(match.Groups[5].Value);
                var xorout        = ulong.Parse(match.Groups[6].Value, NumberStyles.HexNumber);
                var check         = ulong.Parse(match.Groups[7].Value, NumberStyles.HexNumber);
                var parameter     = new CrcParameters(size, poly, init, xorout, refin, refout, check);
                parameters.Add(parameter);
            }
            return(parameters);
        }
Exemplo n.º 10
0
 public void CrcParameterAliasesTest(CrcStandard alias, CrcStandard master)
 {
     Assert.AreEqual(CrcParameters.GetParameters(master), CrcParameters.GetParameters(alias));
 }
Exemplo n.º 11
0
        static System.Security.Cryptography.HashAlgorithm GetHashAlgorithm(Type type)
        {
            switch (type)
            {
            case Type.Adler32: return(new Adler32());

            case Type.APHash: return(new APHash());

            case Type.BkdrHash: return(new BkdrHash());

            case Type.Cksum: return(new Cksum());

            case Type.Crc_8Bit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc8Bit)));

            case Type.Crc_8BitIcode: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc8BitIcode)));

            case Type.Crc_8BitItu: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc8BitItu)));

            case Type.Crc_8BitMaxim: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc8BitMaxim)));

            case Type.Crc_8BitWcdma: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc8BitWcdma)));

            case Type.Crc_16Bit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16Bit)));

            case Type.Crc_16BitCcitt: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitCcitt)));

            case Type.Crc_16BitKermit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitKermit)));

            case Type.Crc_16BitMaxim: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitMaxim)));

            case Type.Crc_16BitUsb: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitUsb)));

            case Type.Crc_16BitArc: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitArc)));

            case Type.Crc_16BitIbm: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitIbm)));

            case Type.Crc_16BitLha: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitLha)));

            case Type.Crc_16BitCcittFalse: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitCcittFalse)));

            case Type.Crc_16BitX25: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitX25)));

            case Type.Crc_16BitXkermit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitXkermit)));

            case Type.Crc_16BitXmodem: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitXmodem)));

            case Type.Crc_16BitZmodem: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc16BitZmodem)));

            case Type.Crc_24Bit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc24Bit)));

            case Type.Crc_24BitOpenPgp: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc24BitOpenPgp)));

            case Type.Crc_32Bit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)));

            case Type.Crc_32BitBzip2: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitBzip2)));

            case Type.Crc_32BitPkzip: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitPkzip)));

            case Type.Crc_32BitIscsi: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitIscsi)));

            case Type.Crc_32BitItu: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitItu)));

            case Type.Crc_32BitJam: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitJam)));

            case Type.Crc_32BitMpeg2: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitMpeg2)));

            case Type.Crc_32BitPosix: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitPosix)));

            case Type.Crc_32BitCksum: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32BitCksum)));

            case Type.Crc_64Bit: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc64Bit)));

            case Type.Crc_64BitWE: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc64BitWE)));

            case Type.Crc_64BitIso: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc64BitIso)));

            case Type.Crc_64BitJones: return(new Crc(CrcParameters.GetParameters(CrcStandard.Crc64BitJones)));

            case Type.Dha256: return(new Dha256());

            case Type.DjbHash: return(new DjbHash());

            case Type.ElfHash: return(new ElfHash());

            case Type.Fcs16: return(new Fcs16());

            case Type.Fcs32: return(new Fcs32());

            case Type.Fletcher_8: return(new Fletcher(FletcherParameters.GetParameters(FletcherStandard.Fletcher8Bit)));

            case Type.Fletcher_16: return(new Fletcher(FletcherParameters.GetParameters(FletcherStandard.Fletcher16Bit)));

            case Type.Fletcher_32: return(new Fletcher(FletcherParameters.GetParameters(FletcherStandard.Fletcher32Bit)));

            case Type.FNV_32_0: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv32BitType0)));

            case Type.FNV_32_1: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv32BitType1)));

            case Type.FNV_32_1a: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv32BitType1A)));

            case Type.FNV_64_0: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv64BitType0)));

            case Type.FNV_64_1: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv64BitType1)));

            case Type.FNV_64_1a: return(new Fnv(FnvParameters.GetParameters(FnvStandard.Fnv64BitType1A)));

            case Type.GHash3: return(new GHash(GHashParameters.GetParameters(GHashStandard.GHash3)));

            case Type.GHash5: return(new GHash(GHashParameters.GetParameters(GHashStandard.GHash5)));

            case Type.GOST: return(new GostHash());

            case Type.HAS160: return(new Has160());

            case Type.HAVAL_3_128: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval128Bit3Pass)));

            case Type.HAVAL_4_128: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval128Bit4Pass)));

            case Type.HAVAL_5_128: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval128Bit5Pass)));

            case Type.HAVAL_3_160: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval160Bit3Pass)));

            case Type.HAVAL_4_160: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval160Bit4Pass)));

            case Type.HAVAL_5_160: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval160Bit5Pass)));

            case Type.HAVAL_3_192: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval192Bit3Pass)));

            case Type.HAVAL_4_192: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval192Bit4Pass)));

            case Type.HAVAL_5_192: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval192Bit5Pass)));

            case Type.HAVAL_3_224: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval224Bit3Pass)));

            case Type.HAVAL_4_224: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval224Bit4Pass)));

            case Type.HAVAL_5_224: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval224Bit5Pass)));

            case Type.HAVAL_3_256: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval256Bit3Pass)));

            case Type.HAVAL_4_256: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval256Bit4Pass)));

            case Type.HAVAL_5_256: return(new Haval(HavalParameters.GetParameters(HavalStandard.Haval256Bit5Pass)));

            case Type.JenkinsHash: return(new JenkinsHash());

            case Type.JSHash: return(new JSHash());

            case Type.MD2: return(new MD2());

            case Type.MD4: return(new MD4());

            case Type.MD5: return(new MD5());

            case Type.Panama: return(new Panama());

            case Type.Pjw32: return(new Pjw32());

            case Type.RIPEMD128: return(new RipeMD128());

            case Type.RIPEMD160: return(new RipeMD160());

            case Type.RIPEMD256: return(new RipeMD256());

            case Type.RIPEMD320: return(new RipeMD320());

            case Type.RSHash: return(new RSHash());

            case Type.SdbmHash: return(new SdbmHash());

            case Type.SHA0: return(new Sha0());

            case Type.SHA1: return(new Sha1());

            case Type.SHA224: return(new Sha224());

            case Type.SHA256: return(new Sha256());

            case Type.SHA384: return(new Sha384());

            case Type.SHA512: return(new Sha512());

            case Type.Snefru_4_128: return(new Snefru2(Snefru2Parameters.GetParameters(Snefru2Standard.Snefru128Bit4Pass)));

            case Type.Snefru_4_256: return(new Snefru2(Snefru2Parameters.GetParameters(Snefru2Standard.Snefru256Bit4Pass)));

            case Type.Snefru_8_128: return(new Snefru2(Snefru2Parameters.GetParameters(Snefru2Standard.Snefru128Bit8Pass)));

            case Type.Snefru_8_256: return(new Snefru2(Snefru2Parameters.GetParameters(Snefru2Standard.Snefru256Bit8Pass)));

            case Type.Sum_8: return(new Sum(SumParameters.GetParameters(SumStandard.Sum8Bit)));

            case Type.Sum_16: return(new Sum(SumParameters.GetParameters(SumStandard.Sum16Bit)));

            case Type.Sum_24: return(new Sum(SumParameters.GetParameters(SumStandard.Sum24Bit)));

            case Type.Sum_32: return(new Sum(SumParameters.GetParameters(SumStandard.Sum32Bit)));

            case Type.Sum_64: return(new Sum(SumParameters.GetParameters(SumStandard.Sum64Bit)));

            case Type.SumBsd: return(new SumBsd());

            case Type.SumSysV: return(new SumSysV());

            case Type.Tiger_3_128: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger128BitVersion1)));

            case Type.Tiger_3_160: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger160BitVersion1)));

            case Type.Tiger_3_192: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger192BitVersion1)));

            case Type.Tiger2_128: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger128BitVersion2)));

            case Type.Tiger2_160: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger160BitVersion2)));

            case Type.Tiger2_192: return(new Tiger(TigerParameters.GetParameters(TigerStandard.Tiger192BitVersion2)));

            case Type.Whirlpool: return(new Whirlpool());

            case Type.Xum32: return(new Xum32());

            case Type.Xor8: return(new Xor8());

            default: throw new NotImplementedException();
            }
        }
 public static SimpleCrcInputStream <T> Wrap <T>(this CrcParameters <T> parameters, IAsyncSimpleInputStream <byte> @base, bool leaveBaseUndisposed = false)
 {
     return(new SimpleCrcInputStream <T>(@base, parameters, leaveBaseUndisposed));
 }
Exemplo n.º 13
0
 public void ConstructorBadInitTest()
 {
     HashChain chain = new HashChain(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), TestVectors.Battery.Numeric, 0);
 }
Exemplo n.º 14
0
 public SimpleCrcInputStream(IAsyncSimpleInputStream <byte> @base, CrcParameters <T> crcParameters, bool leaveBaseUndisposed = false) :
     this(@base, crcParameters.CreateCalculator(), leaveBaseUndisposed)
 {
 }
Exemplo n.º 15
0
 protected bool Equals(CrcParameters other)
 {
     return(Size == other.Size && Polynomial == other.Polynomial && InitialValue == other.InitialValue && FinalXorValue == other.FinalXorValue && ReflectInput == other.ReflectInput && ReflectOutput == other.ReflectOutput && ExpectedCheck == other.ExpectedCheck);
 }
Exemplo n.º 16
0
 public void EqualsNullTest()
 {
     Assert.IsFalse(CrcParameters.GetParameters(CrcStandard.Crc32Bit).Equals(null));
 }
Exemplo n.º 17
0
 public void CrcOrderTest(int order)
 {
     CrcParameters param = new CrcParameters(order, 1, 1, 1, true);
 }
Exemplo n.º 18
0
        public void BadIndexTest()
        {
            HashChain chain = new HashChain(new Crc(CrcParameters.GetParameters(CrcStandard.Crc32Bit)), TestVectors.Battery.Numeric);

            byte[] test = chain[-1];
        }