Skip to content

Instantly share code, notes, and snippets.

@sopanatx
Created December 17, 2022 04:19
Show Gist options
  • Save sopanatx/70812f71d868e3157577a4effd0f36f5 to your computer and use it in GitHub Desktop.
Save sopanatx/70812f71d868e3157577a4effd0f36f5 to your computer and use it in GitHub Desktop.
@Override
@NotNull
public String encrypt(@NotNull String plainText, @NotNull String key) {
Intrinsics.checkNotNullParameter(plainText, "plainText");
Intrinsics.checkNotNullParameter(key, "key");
Cipher cipher = getCipher(1, key);
byte[] plainTextBytes = plainText.getBytes(Charsets.UTF_8);
Intrinsics.checkNotNullExpressionValue(plainTextBytes, "(this as java.lang.String).getBytes(charset)");
byte[] encryptedBytes = Base64.encode(cipher.doFinal(plainTextBytes));
Intrinsics.checkNotNullExpressionValue(encryptedBytes, "encode(encryptedBytes)");
return StringsKt__StringsJVMKt.replace$default(new String(encryptedBytes, Charsets.UTF_8), "(\\r|\\n|\\r\\n)+", "", false, 4, (Object) null);
}
@Override
@NotNull
public String generateRandomString(int length) {
SecureRandom random = new SecureRandom();
StringBuilder builder = new StringBuilder(length);
char[] characters = this.characterArray.getCharacters();
if (length > 0) {
int i = 0;
do {
i++;
builder.append(characters[random.nextInt(characters.length)]);
} while (i < length);
}
return builder.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment