decrypt method
Decrypts the encryptedText
encrypted by encrypt().
saltLength
: this is the length of the salt preceding the encryptedText
.
Implementation
String decrypt(String encryptedText,
{int saltLength = 4,
CharClass charClass = CharClass.chars95,
String? charSetMembers}) {
var rc = '';
final salt = encryptedText.substring(0, saltLength);
final encryptedText2 = encryptedText.substring(saltLength);
final currentState = firstState.toList();
currentState[0] =
random.maskOperand(random.maskOperand(salt.hashCode) + currentState[0]);
random.restoreState(currentState);
charSetMembers ??= BaseRandom.getCharClassMembers(charClass);
if (charClass == CharClass.chars95 || charClass == CharClass.chars96) {
// the charset is ordered like ASCII:
final upperBound = charClass == CharClass.chars95 ? 127 : 128;
final max = charClass == CharClass.chars95 ? 95 : 96;
for (var ix = 0; ix < encryptedText2.length; ix++) {
final code = encryptedText2.codeUnitAt(ix);
if (code < 32 || code >= upperBound) {
rc += encryptedText2[ix];
} else {
final rand = random.nextInt(max: max);
final value = 32 + (code - 32 + max - rand) % max;
rc += String.fromCharCode(value);
}
}
} else {
// the charset is not ordered like ASCII:
// charSetMembers cannot be null!
final membersLength = charSetMembers?.length ?? 1;
for (var ix = 0; ix < encryptedText2.length; ix++) {
final index = charSetMembers?.indexOf(encryptedText2[ix]) ?? -1;
if (index < 0) {
rc += encryptedText2[ix];
} else {
final rand = random.nextInt(max: membersLength);
final value = (index + membersLength - rand) % membersLength;
// charSetMembers cannot be null!
rc += charSetMembers == null ? '' : charSetMembers[value];
}
}
}
return rc;
}