コード例 #1
0
        public void Init(string passkey)
        {
            //0-4
            AssertNextBytesEqual(Safe.Tag);

            //5-36
            var salt = ReadBytes(Safe.SaltLengthV3);

            //37-40
            var iterations = ReadUInt32();

            var keyCipher = new KeyCipher(_crypto, false, passkey, salt, iterations);

            //41-72
            var expectedKeyHash = ReadBytes(Safe.StretchedPasskeyHashLength);

            if (!keyCipher.ValidateHashedKey(expectedKeyHash))
            {
                throw new InvalidPasskeyException();
            }

            //73-104
            var dataKey = Decrypt(keyCipher, Safe.DataKeyLength);

            //105-136
            var hmacKey = Decrypt(keyCipher, Safe.HmacKeyLength);

            //137-152
            var dataInitializationVector = ReadBytes(Safe.DataInitializationVectorLength);

            _dataCipher = new DataCipher(false, dataKey, dataInitializationVector);

            _hmac = _crypto.HMACSHA256Factory.From(hmacKey);
        }
コード例 #2
0
        public void Init(string passkey, UInt32 iterations)
        {
            WriteBytes(Safe.Tag);

            var salt   = GetRandomData(Safe.SaltLengthV3);
            var sha256 = _crypto.SHA256ManagedFactory.New();

            if (salt.Length != Safe.SaltLengthV3)
            {
                throw new Exception("Invalid salt length");
            }
            salt = sha256.ComputeHash(salt);
            WriteBytes(salt);
            WriteBytes(BitConverter.GetBytes(iterations));
            var keyCipher = new KeyCipher(_crypto, true, passkey, salt, iterations);

            var keyHash = keyCipher.GetHashedKey();

            if (keyHash.Length != Safe.StretchedPasskeyHashLength)
            {
                throw new Exception("Invalid hashed key length");
            }
            WriteBytes(keyHash);

            var dataKey          = GetRandomData(Safe.DataKeyLength);
            var encryptedDataKey = new byte[dataKey.Length];
            var l1 = keyCipher.ProcessBytes(dataKey, 0, dataKey.Length, encryptedDataKey, 0);

            keyCipher.DoFinal(encryptedDataKey, l1);
            WriteBytes(encryptedDataKey);

            var hmacKey          = GetRandomData(Safe.HmacKeyLength);
            var encryptedHmacKey = new byte[hmacKey.Length];

            l1 = keyCipher.ProcessBytes(hmacKey, 0, encryptedHmacKey.Length, encryptedHmacKey, 0);
            keyCipher.DoFinal(encryptedHmacKey, l1);
            WriteBytes(encryptedHmacKey);

            var dataInitializationVector = GetRandomData(Safe.DataInitializationVectorLength);
            var hasher = _crypto.SHA256ManagedFactory.New();
            var hashedDataInitializationVector = hasher.ComputeHash(dataInitializationVector);

            Buffer.BlockCopy(hashedDataInitializationVector, 0, dataInitializationVector, 0, Safe.DataInitializationVectorLength);
            WriteBytes(dataInitializationVector);

            _dataCipher = new DataCipher(true, dataKey, dataInitializationVector);

            _hmac = _crypto.HMACSHA256Factory.From(hmacKey);
        }