} // end cctr /// <summary> /// Initialise the <see cref="PBKDF_Argon2NotBuildInAdapter" /> /// from the password and parameters. /// </summary> /// <param name="a_Password"> /// the password to use. /// </param> /// <param name="a_Parameters"> /// Argon2 configuration. /// </param> internal PBKDF_Argon2NotBuildInAdapter(byte[] a_Password, IArgon2Parameters a_Parameters) { if (a_Password == null) { throw new ArgumentNullHashLibException(nameof(a_Password)); } ValidatePBKDF_Argon2Inputs(a_Parameters); Password = a_Password.DeepCopy(); Parameters = a_Parameters; if (Parameters.Lanes < MIN_PARALLELISM) { throw new ArgumentInvalidHashLibException( String.Format(Global.LanesTooSmall, MIN_PARALLELISM)); } else if (Parameters.Lanes > MAX_PARALLELISM) { throw new ArgumentInvalidHashLibException( String.Format(Global.LanesTooBig, MAX_PARALLELISM)); } else if (Parameters.Memory < (2 * Parameters.Lanes)) { throw new ArgumentInvalidHashLibException( String.Format(Global.MemoryTooSmall, 2 * Parameters.Lanes, 2 * Parameters.Lanes)); } else if (Parameters.Iterations < MIN_ITERATIONS) { throw new ArgumentInvalidHashLibException( String.Format(Global.IterationsTooSmall, MIN_ITERATIONS)); } DoInit(a_Parameters); } // end cctr
} // end function Clear private byte[] InitialHash(IArgon2Parameters a_Parameters, Int32 a_OutputLength, byte[] a_Password) { IHash LBlake2B = MakeBlake2BInstanceAndInitialize(ARGON2_PREHASH_DIGEST_LENGTH); AddIntToLittleEndian(LBlake2B, a_Parameters.Lanes); AddIntToLittleEndian(LBlake2B, a_OutputLength); AddIntToLittleEndian(LBlake2B, a_Parameters.Memory); AddIntToLittleEndian(LBlake2B, a_Parameters.Iterations); AddIntToLittleEndian(LBlake2B, (Int32)a_Parameters.Version); AddIntToLittleEndian(LBlake2B, (Int32)a_Parameters.Type); AddByteString(LBlake2B, a_Password); AddByteString(LBlake2B, a_Parameters.Salt); AddByteString(LBlake2B, a_Parameters.Secret); AddByteString(LBlake2B, a_Parameters.Additional); return(LBlake2B.TransformFinal().GetBytes()); } // end function InitialHash
} // end function InitializeMemory private void DoInit(IArgon2Parameters a_Parameters) { Int32 MemoryBlocks; // 2. Align memory size // Minimum memoryBlocks = 8L blocks, where L is the number of lanes */ MemoryBlocks = a_Parameters.Memory; if (MemoryBlocks < (2 * ARGON2_SYNC_POINTS * a_Parameters.Lanes)) { MemoryBlocks = 2 * ARGON2_SYNC_POINTS * a_Parameters.Lanes; } SegmentLength = MemoryBlocks / (Parameters.Lanes * ARGON2_SYNC_POINTS); LaneLength = SegmentLength * ARGON2_SYNC_POINTS; // Ensure that all segments have equal length MemoryBlocks = SegmentLength * (a_Parameters.Lanes * ARGON2_SYNC_POINTS); InitializeMemory(MemoryBlocks); } // end function DoInit
public static IPBKDF_Argon2 CreatePBKDF_Argon2(byte[] a_Password, IArgon2Parameters a_Argon2Parameters) => new PBKDF_Argon2NotBuildInAdapter(a_Password, a_Argon2Parameters);