/// <summary> Decrypts the bytes with the current password and salt </summary> public byte[] Decrypt(byte[] blob, Salt.Size szSaltSize) { try { using (SaltedData data = new SaltedData(blob, szSaltSize)) using (AESCryptoKey key = CreateKey(data.Salt)) return(key.Decrypt(data.GetDataBytes())); } catch (InvalidOperationException) { throw; } catch { throw CryptographicException(); } }
/// <summary> Returns the Salt and Data as a stream </summary> public Stream ToStream() { return(SaltedData.CombineStream(this.Salt, this.GetDataStream())); }
/// <summary> Creates a salted hash from the given bytes and salt </summary> public PasswordHash(Stream bytes, Salt salt) { using (bytes) using (HashDerivedBytes <HMACSHA256> hashBytes = new HashDerivedBytes <HMACSHA256>(bytes, salt, StandardIterations)) _hash = new SaltedData(salt, hashBytes.GetBytes(32)); }
/// <summary> Recreates a hash </summary> private PasswordHash(SaltedData hash) { _hash = hash; }
public void TestSaltedDataStream() { Salt s = new Salt(Salt.Size.b64); byte[] testData = new byte[8]; byte[] test1 = new SaltedData(s, testData).ToArray(); Assert.AreEqual(16, test1.Length); byte[] test2 = IOStream.ReadAllBytes(new SaltedData(s, testData).ToStream()); Assert.AreEqual(16, test2.Length); byte[] test3 = IOStream.ReadAllBytes(SaltedData.CombineStream(s, new MemoryStream(testData))); Assert.AreEqual(16, test3.Length); Assert.AreEqual(test1, test2); Assert.AreEqual(test1, test3); Assert.AreEqual(test2, test3); }
public void TestSaltedDataWithSpecificSize() { Salt s = new Salt(Salt.Size.b64); byte[] testData = new byte[8]; new Random().NextBytes(testData); byte[] tmp; using (SaltedData sd = new SaltedData(s, testData)) { Assert.AreEqual(16, sd.Length); Assert.AreEqual(s, sd.Salt); Assert.AreEqual(testData, sd.GetDataBytes()); tmp = sd.ToArray(); Assert.AreEqual(16, tmp.Length); Assert.AreEqual(tmp, IOStream.ReadAllBytes(sd.ToStream())); } using (SaltedData sd = new SaltedData(s, new MemoryStream(testData))) { Assert.AreEqual(s, sd.Salt); Assert.AreEqual(testData, sd.GetDataBytes()); Assert.AreEqual(tmp, sd.ToArray()); } using (SaltedData sd = new SaltedData(tmp, Salt.Size.b64)) { Assert.AreEqual(s, sd.Salt); Assert.AreEqual(testData, sd.GetDataBytes()); Assert.AreEqual(tmp, sd.ToArray()); } }
/// <summary> Decrypts the bytes with the current password and salt </summary> public byte[] Decrypt(byte[] blob, Salt.Size szSaltSize) { try { using (SaltedData data = new SaltedData(blob, szSaltSize)) using (AESCryptoKey key = CreateKey(data.Salt)) return key.Decrypt(data.GetDataBytes()); } catch (InvalidOperationException) { throw; } catch { throw CryptographicException(); } }