示例#1
0
        public static byte[] ComputeHMACSHA384HashFromFile(string filePath, byte[] authKey, long startPosition, long endPosition)
        {
            byte[] hash = null;

            using (var fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                fStream.Position = startPosition;
                byte[] buffer = new byte[(1024 * 4)];
                long   amount = (endPosition - startPosition);

                using (var hmacSha384 = new HMACSHA384(authKey))
                {
                    while (amount > 0)
                    {
                        int bytesRead = fStream.Read(buffer, 0, (int)Math.Min(buffer.Length, amount));

                        if (bytesRead > 0)
                        {
                            amount -= bytesRead;

                            if (amount > 0)
                            {
                                hmacSha384.TransformBlock(buffer, 0, bytesRead, buffer, 0);
                            }
                            else
                            {
                                hmacSha384.TransformFinalBlock(buffer, 0, bytesRead);
                            }
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }
                    }

                    hash = hmacSha384.Hash;
                }
            }

            return(hash);
        }
示例#2
0
        public BDCrypto(string Password)
        {
            if (!Info.Moduls.Contains("Crypto/BDCrypto.cs"))
            {
                Info.Moduls.Add("Crypto/BDCrypto.cs");
            }

            OneKeyHasher Translator = new OneKeyHasher();

            Encryption = new Dictionary <byte, byte>();
            Decryption = new Dictionary <byte, byte>();

            byte[] TMPStore;

            using (SHA384Cng Seg2 = new SHA384Cng())
                using (HMACSHA384 Seg1 = new HMACSHA384(Encoding.UTF32.GetBytes(Password)))
                {
                    Seg1.Initialize();
                    Translator.TheHashSize    = 512;
                    Translator.TheCharsetUsed = Encoding.UTF8;
                    TMPStore = Translator.Hash(Password);

                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    Seg1.TransformBlock(TMPStore, 0, TMPStore.Length, TMPStore, 0);
                    TMPStore = Seg1.TransformFinalBlock(TMPStore, 0, TMPStore.Length);
                    TMPStore = Seg2.ComputeHash(TMPStore);
                }

            Translator.TheCharsetUsed = Encoding.Unicode;
            Translator.TheHashSize    = 255;
            TMPStore = Translator.Hash(TMPStore);

            int  pos = 0;
            byte tmp = 0;

            while (true)
            {
                if (!Encryption.ContainsValue(TMPStore[pos]) && !Decryption.ContainsKey(TMPStore[pos]))
                {
                    Encryption.Add(tmp, TMPStore[pos]);
                    Decryption.Add(TMPStore[pos], tmp);
                    if (tmp == 255)
                    {
                        break;
                    }
                    else
                    {
                        tmp++;
                    }
                }

                pos++;
                if (pos == TMPStore.Length)
                {
                    TMPStore = Translator.Hash(TMPStore);
                    pos      = 0;
                }
            }

            _Key   = new byte[256];
            Layers = 5;

            byte x = 0;

            while (true)
            {
                _Key[x] = Encryption[x];
                if (x == 255)
                {
                    break;
                }
                else
                {
                    x++;
                }
            }
        }