public class ScryptCipherProvider extends RandomIVPBECipherProvider
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_N
These values can be calculated automatically using the code
ScryptCipherProviderGroovyTest#calculateMinimumParameters or manually updated by a maintainer |
private static int |
DEFAULT_P |
private static int |
DEFAULT_R |
private static org.slf4j.Logger |
logger |
private static Pattern |
MCRYPT_SALT_FORMAT |
private int |
n |
private int |
p |
private int |
r |
private static Pattern |
SCRYPT_SALT_FORMAT |
IV_DELIMITER, MAX_IV_LIMIT, MAX_SALT_LIMIT, SALT_DELIMITER
Constructor and Description |
---|
ScryptCipherProvider()
Instantiates a Scrypt cipher provider with the default parameters N=2^14, r=8, p=1.
|
ScryptCipherProvider(int n,
int r,
int p)
Instantiates a Scrypt cipher provider with the specified N, r, p values.
|
Modifier and Type | Method and Description |
---|---|
static byte[] |
extractRawSaltFromScryptSalt(String scryptSalt)
Returns the raw salt contained in the provided Scrypt salt string.
|
String |
formatSaltForScrypt(byte[] salt)
Formats the salt into a string which Scrypt can understand containing the N, r, p values along with the salt
value.
|
static String |
formatSaltForScrypt(byte[] salt,
int n,
int r,
int p)
Formats the salt into a string which Scrypt can understand containing the N, r, p values along with the salt
value.
|
byte[] |
generateSalt()
Returns a random salt suitable for this cipher provider.
|
Cipher |
getCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength,
boolean encryptMode)
Returns an initialized cipher for the specified algorithm.
|
Cipher |
getCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
int keyLength,
boolean encryptMode)
Returns an initialized cipher for the specified algorithm.
|
int |
getDefaultSaltLength()
Returns the default salt length for this implementation.
|
protected Cipher |
getInitializedCipher(EncryptionMethod encryptionMethod,
String password,
byte[] salt,
byte[] iv,
int keyLength,
boolean encryptMode) |
(package private) org.slf4j.Logger |
getLogger() |
protected int |
getN() |
protected int |
getP() |
protected int |
getR() |
static boolean |
isPValid(int r,
int p)
Returns whether the provided parallelization factor (p value) is within boundaries.
|
static boolean |
isRValid(int r)
Returns whether the provided block size (r value) is a positive integer or not.
|
static boolean |
isScryptFormattedSalt(String salt)
Returns
true if the salt string is a valid Scrypt salt string ($s0$e0801$abcdefghi..{22} ). |
private void |
parseSalt(String scryptSalt,
byte[] rawSalt,
List<Integer> params) |
static String |
translateSalt(String mcryptSalt)
Translates a salt from the mcrypt format
$n$r$p$salt_hex to the Java scrypt format $s0$params$saltBase64 . |
readIV, readSalt, writeIV, writeSalt
private static final org.slf4j.Logger logger
private final int n
private final int r
private final int p
private static final int DEFAULT_N
ScryptCipherProviderGroovyTest#calculateMinimumParameters
or manually updated by a maintainerprivate static final int DEFAULT_R
private static final int DEFAULT_P
private static final Pattern SCRYPT_SALT_FORMAT
private static final Pattern MCRYPT_SALT_FORMAT
public ScryptCipherProvider()
public ScryptCipherProvider(int n, int r, int p)
n
- the number of iterationsr
- the block size in bytesp
- the parallelization factorpublic static boolean isPValid(int r, int p)
r
- the block size in bytesp
- the parallelization factorpublic static boolean isRValid(int r)
r
- the block size in bytespublic Cipher getCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode) throws Exception
getCipher
in class RandomIVPBECipherProvider
encryptionMethod
- the EncryptionMethod
password
- the secret inputsalt
- the complete salt (e.g. "$2a$10$gUVbkVzp79H8YaCOsCVZNu".getBytes(StandardCharsets.UTF_8)
)iv
- the IVkeyLength
- the desired key length in bitsencryptMode
- true for encrypt, false for decryptException
- if there is a problem initializing the cipherorg.slf4j.Logger getLogger()
getLogger
in class RandomIVPBECipherProvider
public Cipher getCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, int keyLength, boolean encryptMode) throws Exception
The IV can be retrieved by the calling method using Cipher.getIV()
.
encryptionMethod
- the EncryptionMethod
password
- the secret inputsalt
- the complete salt (e.g. "$s0$20101$gUVbkVzp79H8YaCOsCVZNu".getBytes(StandardCharsets.UTF_8)
)keyLength
- the desired key length in bitsencryptMode
- true for encrypt, false for decryptException
- if there is a problem initializing the cipherprotected Cipher getInitializedCipher(EncryptionMethod encryptionMethod, String password, byte[] salt, byte[] iv, int keyLength, boolean encryptMode) throws Exception
Exception
public static byte[] extractRawSaltFromScryptSalt(String scryptSalt)
scryptSalt
- the full Scrypt saltpublic static boolean isScryptFormattedSalt(String salt)
true
if the salt string is a valid Scrypt salt string ($s0$e0801$abcdefghi..{22}
).salt
- the salt string to evaluatepublic String formatSaltForScrypt(byte[] salt)
The salt is expected to be in the format new String(saltBytes, StandardCharsets.UTF_8) => "$s0$e0801$ABCDEF...."
.
salt
- the provided saltpublic static String formatSaltForScrypt(byte[] salt, int n, int r, int p)
The salt is expected to be in the format new String(saltBytes, StandardCharsets.UTF_8) => "$s0$e0801$ABCDEF...."
.
salt
- the provided saltn
- the N paramr
- the r paramp
- the p parampublic static String translateSalt(String mcryptSalt)
$n$r$p$salt_hex
to the Java scrypt format $s0$params$saltBase64
.mcryptSalt
- the mcrypt-formatted salt stringpublic byte[] generateSalt()
PBECipherProvider
PBECipherProvider.getDefaultSaltLength()
public int getDefaultSaltLength()
PBECipherProvider
protected int getN()
protected int getR()
protected int getP()
Copyright © 2021 Apache NiFi Project. All rights reserved.