public ModularCrypt(char delim, string identifier, int workFactor, byte[] salt, byte[] cipher) { if (INVALID_DELIM_ALPHABET.Contains(delim)) { throw new ArgumentException("Invalid delim character.", "modularCryptFormatStr"); } if (identifier == null) { throw new ArgumentException("Cannot be null.", "identifier"); } if (workFactor < 0) { throw new ArgumentException("Must be a non-negative integer.", "workFactor"); } if (salt == null) { throw new ArgumentException("Cannot be null.", "salt"); } _delim = delim; _identifier = identifier; _workFactor = workFactor; _salt = salt; _cipher = cipher; }
public ModularCrypt(string modularCryptFormatStr) { if (string.IsNullOrWhiteSpace(modularCryptFormatStr)) { throw new ArgumentException("Cannot be null, empty or whitespace.", "modularCryptFormatStr"); } // First character is the delim character char delim = modularCryptFormatStr[0]; if (INVALID_DELIM_ALPHABET.Contains(delim)) { throw new ArgumentException("Delim character not allowed. '" + delim + "'"); } // Split into ModularCrypt segments string[] mcSegments = modularCryptFormatStr.Split(new[] { delim }, StringSplitOptions.None); if (mcSegments.Length - 1 != 4) { throw new ArgumentException("Invalid ModularCryptFormat String: " + modularCryptFormatStr, "modularCryptFormatStr"); } // WorkFactor part must be a non-negative integer int workFactor; if (!int.TryParse(mcSegments[FORMAT_WORKFACTOR_INDEX], out workFactor) || workFactor < 0) { throw new ArgumentException("WorkFactor segment must be a non-negative integer", "modularCryptFormatStr"); } // Salt part must be a Base64 string byte[] salt; try { salt = Convert.FromBase64String(mcSegments[FORMAT_SALT_INDEX]); } catch (FormatException) { throw new ArgumentException("Invalid Salt segment", "modularCryptFormatStr"); } // Cipher part must be empty or a Base64 string byte[] cipher; try { cipher = mcSegments[FORMAT_CIPHER_INDEX] != string.Empty ? Convert.FromBase64String(mcSegments[FORMAT_CIPHER_INDEX]) : new byte[] { }; } catch (IndexOutOfRangeException) { cipher = new byte[] { }; } catch (ArgumentNullException) { throw new ArgumentException("Invalid Cipher component", "modularCryptFormatStr"); } catch (FormatException) { throw new ArgumentException("Invalid Cipher component", "modularCryptFormatStr"); } _delim = delim; _identifier = mcSegments[FORMAT_IDENTIFIER_INDEX]; _workFactor = workFactor; _salt = salt; _cipher = cipher; }