Exemplo n.º 1
0
        public static byte[] HashRaw(EHashType type, byte[] bs, int index, int length)
        {
            if (bs == null)
            {
                return(null);
            }

            HashAlgorithm cmd5 = null;

            switch (type)
            {
            case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;

            case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;

            case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;

            case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;

            case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            bs = cmd5.ComputeHash(bs, index, length);
            cmd5.Dispose();
            return(bs);
        }
Exemplo n.º 2
0
        public static byte[] HashRaw(EHashType type, Stream bs, bool seekBegin)
        {
            if (bs == null)
            {
                return(null);
            }

            HashAlgorithm cmd5 = null;

            switch (type)
            {
            case EHashType.Md5: cmd5 = new MD5CryptoServiceProvider(); break;

            case EHashType.Sha1: cmd5 = new SHA1CryptoServiceProvider(); break;

            case EHashType.Sha256: cmd5 = new SHA256CryptoServiceProvider(); break;

            case EHashType.Sha384: cmd5 = new SHA384CryptoServiceProvider(); break;

            case EHashType.Sha512: cmd5 = new SHA512CryptoServiceProvider(); break;
            }

            if (seekBegin)
            {
                bs.Seek(0, SeekOrigin.Begin);
            }

            byte[] bsh = cmd5.ComputeHash(bs);
            cmd5.Dispose();
            return(bsh);
        }
Exemplo n.º 3
0
        private static bool VerifyChecksum(byte[] raw, XISFChecksumTypeEnum cksumType, string providedCksum)
        {
            string      computedCksum;
            SHA3Managed sha3;

            using (MyStopWatch.Measure($"XISF Checksum = {cksumType}")) {
                switch (cksumType)
                {
                case XISFChecksumTypeEnum.SHA1:
                    SHA1 sha1 = new SHA1CryptoServiceProvider();
                    computedCksum = GetStringFromHash(sha1.ComputeHash(raw));
                    sha1.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA256:
                    SHA256 sha256 = new SHA256CryptoServiceProvider();
                    computedCksum = GetStringFromHash(sha256.ComputeHash(raw));
                    sha256.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA512:
                    SHA512 sha512 = new SHA512CryptoServiceProvider();
                    computedCksum = GetStringFromHash(sha512.ComputeHash(raw));
                    sha512.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA3_256:
                    sha3          = new SHA3Managed(256);
                    computedCksum = GetStringFromHash(sha3.ComputeHash(raw));
                    sha3.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA3_512:
                    sha3          = new SHA3Managed(512);
                    computedCksum = GetStringFromHash(sha3.ComputeHash(raw));
                    sha3.Dispose();
                    break;

                default:
                    return(false);
                }
            }
            if (computedCksum.Equals(providedCksum))
            {
                return(true);
            }
            else
            {
                Logger.Error($"XISF: Invalid data block checksum! Expected: {providedCksum} Got: {computedCksum}");
                return(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Hash 值检查
        /// </summary>
        /// <param name="resFile">文件路径</param>
        /// <param name="type">Hash 值的类型</param>
        public CheckHash(string resFile, HashType type)
        {
            hashValue = string.Empty;
            byte[]     retVal = null;
            FileStream file   = new FileStream(resFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            switch (type)
            {
            case HashType.MD5:
                MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
                retVal = MD5Hash.ComputeHash(file);
                MD5Hash.Dispose();
                break;

            case HashType.SHA1:
                SHA1CryptoServiceProvider SHA1Hash = new SHA1CryptoServiceProvider();
                retVal = SHA1Hash.ComputeHash(file);
                SHA1Hash.Dispose();
                break;

            case HashType.SHA256:
                SHA256CryptoServiceProvider SHA256Hash = new SHA256CryptoServiceProvider();
                retVal = SHA256Hash.ComputeHash(file);
                SHA256Hash.Dispose();
                break;

            case HashType.SHA384:
                SHA384CryptoServiceProvider SHA384Hash = new SHA384CryptoServiceProvider();
                retVal = SHA384Hash.ComputeHash(file);
                SHA384Hash.Dispose();
                break;

            case HashType.SHA512:
                SHA512CryptoServiceProvider SHA512Hash = new SHA512CryptoServiceProvider();
                retVal = SHA512Hash.ComputeHash(file);
                SHA512Hash.Dispose();
                break;
            }
            file.Close();

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            hashValue = sb.ToString();
        }
Exemplo n.º 5
0
        /// <summary>
        /// SHA512 加密
        /// </summary>
        public static string Sha512(string dataStr, Encoding encoding)
        {
            if (string.IsNullOrEmpty(dataStr))
            {
                return(string.Empty);
            }

            SHA512 sha512 = new SHA512CryptoServiceProvider();

            byte[]        hashStr = sha512.ComputeHash(encoding.GetBytes(dataStr));
            StringBuilder sb      = new StringBuilder();

            foreach (byte btStr in hashStr)
            {
                sb.AppendFormat("{0:X2}", btStr);
            }
            sha512.Dispose();
            return(sb.ToString());
        }
Exemplo n.º 6
0
        public TripleDesProvider(string seed)
        {
            // build a unique yet reproducable set of characters taken from our seed to use as a salt
            for (int i = 0; i < seed.Length; i++)
            {
                if (i % 3 == 0)
                {
                    _salt += seed[i].ToString();
                }
            }

            // get a 64byte hash of our salt, we will use this as the salt byte array to generate the 24byte hash from our seed
            SHA512CryptoServiceProvider sha = new SHA512CryptoServiceProvider();

            _saltBytes = sha.ComputeHash(Encoding.UTF8.GetBytes(_salt));
            sha.Dispose();

            // generate our final key, and assign to our 3DES object.
            _keyBytes        = new Rfc2898DeriveBytes(seed, _saltBytes, 1000).GetBytes(24);
            _tdesCrypto      = new TripleDESCryptoServiceProvider();
            _tdesCrypto.Key  = _keyBytes;
            _tdesCrypto.Mode = CipherMode.CBC;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Convert the ushort array to a byte arraay, compressing with the requested algorithm if required
        /// </summary>
        /// <param name="data"></param>
        /// <returns>Uncompressed or compressed byte array</returns>
        private byte[] PrepareArray(ushort[] data)
        {
            byte[] outArray;

            /*
             * Convert the ushort[] into a byte[]
             * From here onwards we deal in byte arrays only
             */
            byte[] byteArray = new byte[data.Length * ShuffleItemSize];
            Buffer.BlockCopy(data, 0, byteArray, 0, data.Length * ShuffleItemSize);

            /*
             * Compress the data block as configured.
             */
            using (MyStopWatch.Measure($"XISF Compression = {CompressionType}")) {
                if (CompressionType == XISFCompressionTypeEnum.LZ4)
                {
                    if (ByteShuffling)
                    {
                        CompressionName = "lz4+sh";
                        byteArray       = Shuffle(byteArray, ShuffleItemSize);
                    }
                    else
                    {
                        CompressionName = "lz4";
                    }

                    byte[] tmpArray       = new byte[LZ4Codec.MaximumOutputSize(byteArray.Length)];
                    int    compressedSize = LZ4Codec.Encode(byteArray, 0, byteArray.Length, tmpArray, 0, tmpArray.Length, LZ4Level.L00_FAST);

                    outArray = new byte[compressedSize];
                    Array.Copy(tmpArray, outArray, outArray.Length);
                    tmpArray = null;
                }
                else if (CompressionType == XISFCompressionTypeEnum.LZ4HC)
                {
                    if (ByteShuffling)
                    {
                        CompressionName = "lz4hc+sh";
                        byteArray       = Shuffle(byteArray, ShuffleItemSize);
                    }
                    else
                    {
                        CompressionName = "lz4hc";
                    }

                    byte[] tmpArray       = new byte[LZ4Codec.MaximumOutputSize(byteArray.Length)];
                    int    compressedSize = LZ4Codec.Encode(byteArray, 0, byteArray.Length, tmpArray, 0, tmpArray.Length, LZ4Level.L06_HC);

                    outArray = new byte[compressedSize];
                    Array.Copy(tmpArray, outArray, outArray.Length);
                    tmpArray = null;
                }
                else if (CompressionType == XISFCompressionTypeEnum.ZLIB)
                {
                    if (ByteShuffling)
                    {
                        CompressionName = "zlib+sh";
                        byteArray       = Shuffle(byteArray, ShuffleItemSize);
                    }
                    else
                    {
                        CompressionName = "zlib";
                    }

                    outArray = ZlibStream.CompressBuffer(byteArray);
                }
                else
                {
                    outArray = new byte[byteArray.Length];
                    Array.Copy(byteArray, outArray, outArray.Length);
                    CompressionName = null;
                }
            }

            // Revert to the original data array in case the compression is bigger than uncompressed
            if (outArray.Length > byteArray.Length)
            {
                if (ByteShuffling)
                {
                    //As the original array is shuffled it needs to be unshuffled again - this scenario should be highly unlikely anyways
                    outArray = Unshuffle(byteArray, ShuffleItemSize);
                }
                else
                {
                    outArray = byteArray;
                }
                CompressionType = XISFCompressionTypeEnum.NONE;

                Logger.Debug("XISF output array is larger after compression. Image will be prepared uncompressed instead.");
            }

            if (CompressionType != XISFCompressionTypeEnum.NONE)
            {
                double percentChanged = (1 - ((double)outArray.Length / (double)byteArray.Length)) * 100;
                Logger.Debug($"XISF: {CompressionType} compressed {byteArray.Length} bytes to {outArray.Length} bytes ({percentChanged.ToString("#.##")}%)");
            }

            /*
             * Checksum the data block as configured.
             * If the data block is compressed, we always checksum the compressed form, not the uncompressed form.
             */
            using (MyStopWatch.Measure($"XISF Checksum = {ChecksumType}")) {
                SHA3Managed sha3;

                switch (ChecksumType)
                {
                case XISFChecksumTypeEnum.SHA1:
                    SHA1 sha1 = new SHA1CryptoServiceProvider();
                    Checksum     = GetStringFromHash(sha1.ComputeHash(outArray));
                    ChecksumName = "sha-1";
                    sha1.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA256:
                    SHA256 sha256 = new SHA256CryptoServiceProvider();
                    Checksum     = GetStringFromHash(sha256.ComputeHash(outArray));
                    ChecksumName = "sha-256";
                    sha256.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA512:
                    SHA512 sha512 = new SHA512CryptoServiceProvider();
                    Checksum     = GetStringFromHash(sha512.ComputeHash(outArray));
                    ChecksumName = "sha-512";
                    sha512.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA3_256:
                    sha3         = new SHA3Managed(256);
                    Checksum     = GetStringFromHash(sha3.ComputeHash(outArray));
                    ChecksumName = "sha3-256";
                    sha3.Dispose();
                    break;

                case XISFChecksumTypeEnum.SHA3_512:
                    sha3         = new SHA3Managed(512);
                    Checksum     = GetStringFromHash(sha3.ComputeHash(outArray));
                    ChecksumName = "sha3-512";
                    sha3.Dispose();
                    break;

                case XISFChecksumTypeEnum.NONE:
                default:
                    Checksum     = null;
                    ChecksumName = null;
                    break;
                }
            }

            return(outArray);
        }