static byte[] MFcrypt(byte[] P, byte[] S, int cost, int blockSize, int parallel, int?maxThreads) { int MFLen = blockSize * 128; if (maxThreads == null) { maxThreads = int.MaxValue; } if (!BitMath.IsPositivePowerOf2(cost)) { throw Exceptions.ArgumentOutOfRange("cost", "Cost must be a positive power of 2."); } Check.Range("blockSize", blockSize, 1, int.MaxValue / 128); Check.Range("parallel", parallel, 1, int.MaxValue / MFLen); Check.Range("maxThreads", (int)maxThreads, 1, int.MaxValue); byte[] B = Pbkdf2.ComputeDerivedKey(new HMACSHA256(P), S, 1, parallel * MFLen); uint[] B0 = new uint[B.Length / 4]; for (int i = 0; i < B0.Length; i++) { B0[i] = BitPacking.UInt32FromLEBytes(B, i * 4); } // code is easier with uint[] ThreadSMixCalls(B0, MFLen, cost, blockSize, parallel, (int)maxThreads); for (int i = 0; i < B0.Length; i++) { BitPacking.LEBytesFromUInt32(B0[i], B, i * 4); } Security.Clear(B0); return(B); }
public static void Range(string valueName, int value, int minimum, int maximum) { if (value < minimum || value > maximum) { throw Exceptions.ArgumentOutOfRange(valueName, "Value must be in the range [{0}, {1}].", minimum, maximum); } }
public static void Length(string valueName, Array value, int minimum, int maximum) { Check.Null(valueName, value); if (value.Length < minimum || value.Length > maximum) { throw Exceptions.ArgumentOutOfRange(valueName, "Length must be in the range [{0}, {1}].", minimum, maximum); } }
public static void Bounds(string valueName, Array value, int offset, int count) { Check.Null(valueName, value); if (offset < 0 || count < 0 || count > value.Length - offset) { throw Exceptions.ArgumentOutOfRange(valueName, "Range [{0}, {1}) is outside array bounds [0, {2}).", offset, offset + count, value.Length); } }
/// <inheritdoc /> public override long Seek(long offset, SeekOrigin origin) { long pos; switch (origin) { case SeekOrigin.Begin: pos = offset; break; case SeekOrigin.Current: pos = Position + offset; break; case SeekOrigin.End: pos = Length + offset; break; default: throw Exceptions.ArgumentOutOfRange("origin", "Unknown seek type."); } if (pos < 0) { throw Exceptions.Argument("offset", "Can't seek before the stream start."); } Position = pos; return(pos); }