/// <summary> /// Creates a new PassphrasePrng from a passphrase and salt, /// and seeds it with the output of PKCS5 /// </summary> /// /// <param name="Digest">Digest engine</param> /// <param name="Passphrase">The passphrase</param> /// <param name="Salt">The salt value</param> /// <param name="Iterations">The number of transformation iterations performed by the digest with PKCS5 (default is 10,000)</param> /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called (default is true)</param> /// /// <exception cref="CryptoRandomException">Thrown if a null Digest, Passphrase or Salt are used</exception> public PBPRng(IDigest Digest, byte[] Passphrase, byte[] Salt, int Iterations = PKCS_ITERATIONS, bool DisposeEngine = true) { if (Digest == null) throw new CryptoRandomException("PBPRng:Ctor", "Digest can not be null!", new ArgumentNullException()); if (Passphrase == null) throw new CryptoRandomException("PBPRng:Ctor", "Passphrase can not be null!", new ArgumentNullException()); if (Salt == null) throw new CryptoRandomException("PBPRng:Ctor", "Salt can not be null!", new ArgumentNullException()); try { _disposeEngine = DisposeEngine; PKCS5 pkcs = new PKCS5(Digest, Iterations, false); _digest = Digest; pkcs.Initialize(Salt, Passphrase); _rndData = new byte[_digest.BlockSize]; pkcs.Generate(_rndData); } catch (Exception e) { throw new Exception(e.Message); } _position = 0; }
private void PKCSTest(int Size, int Iterations, byte[] Salt, byte[] Key, byte[] Output) { byte[] outBytes = new byte[Size]; using (PKCS5 gen = new PKCS5(new SHA256(), Iterations)) { gen.Initialize(Salt, Key); gen.Generate(outBytes, 0, Size); } if (Compare.AreEqual(outBytes, Output) == false) throw new Exception("PKCS5: Values are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes)); using (PKCS5 gen = new PKCS5(new SHA256HMAC(), Iterations)) { gen.Initialize(Salt, Key); gen.Generate(outBytes, 0, Size); } if (Compare.AreEqual(outBytes, Output) == false) throw new Exception("PKCS5: Values are not equal! Expected: " + HexConverter.ToString(Output) + " Received: " + HexConverter.ToString(outBytes)); }