CryptoSimple constructor

CryptoSimple({
  1. int? superKey,
  2. int? subKey,
  3. String? secretKey,
  4. EncryptionMode? encryptionMode = EncryptionMode.Normal,
})

CryptoSimple factory constructor : A factory constructor that creates and returns a new instance of the CryptoSimple class. It takes several optional parameters, such as superKey, subKey, secretKey, and encryptionMode, and performs input validation before setting the values of the corresponding instance variables.

Implementation

factory CryptoSimple(
    {int? superKey,
    int? subKey,
    String? secretKey,
    EncryptionMode? encryptionMode = EncryptionMode.Normal}) {
  assert(!_crypto._lock, "CryptoSimple class has already been instantiated.");

  // Check for valid input values
  assert(secretKey == null || secretKey.trim().isNotEmpty,
      "Secret key must not be an empty string.");
  assert(!(secretKey == null && encryptionMode == EncryptionMode.Randomized),
      "EncryptionMode on `Randomized` mode need secretKey.");
  assert(subKey == null || (subKey >= 10 && subKey <= 99),
      "Sub-key must be an integer between 10 and 99.");
  assert(superKey == null || (superKey > 0 && superKey % _maxCharLimit != 0),
      "Super-key must be a positive integer not divisible by $_maxCharLimit.");

  // Set instance variables
  _crypto._superKey = superKey;
  _crypto._subKey = subKey;
  _crypto._secretKey = secretKey;
  _crypto._encryptionMode = encryptionMode;
  _crypto._lock = true;

  // Determine security mode based on input values
  if (_crypto._secretKey != null &&
      _crypto._superKey != null &&
      _crypto._subKey != null) {
    _crypto._securityMode = SecurityMode.SUPER_XOR;
  } else if (_crypto._secretKey != null &&
      _crypto._superKey == null &&
      _crypto._subKey == null) {
    _crypto._securityMode = SecurityMode.XOR;
  } else if (_crypto._superKey != null && _crypto._subKey != null) {
    _crypto._securityMode = SecurityMode.SUPER;
  } else {
    throw ArgumentError("Error in initial data.");
  }

  return _crypto;
}