public class Scrypt extends Object
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_SALT_LENGTH |
private static org.slf4j.Logger |
logger |
private static Pattern |
SCRYPT_PATTERN |
Constructor and Description |
---|
Scrypt() |
Modifier and Type | Method and Description |
---|---|
private static void |
blockmix_salsa8(byte[] by,
int bi,
int yi,
int r) |
private static void |
blockxor(byte[] s,
int si,
byte[] d,
int di,
int len) |
static int |
calculateExpectedMemory(int n,
int r,
int p)
Returns the expected memory cost of the provided parameters in bytes.
|
static boolean |
check(String password,
String hashed)
Compare the supplied plaintext password to a hashed password.
|
protected static byte[] |
deriveScryptKey(byte[] password,
byte[] salt,
int n,
int r,
int p,
int dkLen)
Implementation of the scrypt KDF.
|
private static String |
encodeParams(int n,
int r,
int p) |
private static String |
formatHash(byte[] salt,
int n,
int r,
int p,
byte[] derived) |
static String |
formatSalt(byte[] salt,
int n,
int r,
int p) |
static int |
getDefaultSaltLength() |
private static int |
integerify(byte[] b,
int bi,
int r) |
private static int |
log2(int n) |
static List<Integer> |
parseParameters(String encodedParams)
Parses the individual values from the encoded params value in the modified-mcrypt format for the salt & hash.
|
private static void |
pbkdf2(Mac mac,
byte[] s,
int c,
byte[] dk,
int dkLen)
Implementation of PBKDF2 (RFC2898).
|
private static byte[] |
pbkdf2(String alg,
byte[] p,
byte[] s,
int c,
int dkLen)
Implementation of PBKDF2 (RFC2898).
|
private static int |
r(int a,
int b) |
private static void |
salsa20_8(byte[] b) |
static byte[] |
scrypt(byte[] input,
byte[] salt,
int n,
int r,
int p,
int dkLen)
Hash the supplied input and generate raw unencoded output.
|
static String |
scrypt(String password,
byte[] salt,
int n,
int r,
int p,
int dkLen)
Hash the supplied plaintext password and generate output in the format described
in
scrypt(String, int, int, int, int) . |
static String |
scrypt(String password,
int n,
int r,
int p,
int dkLen)
Hash the supplied plaintext password and generate output in the format described
below:
The hashed output is an
extended implementation of the Modular Crypt Format that also includes the scrypt
algorithm parameters.
|
private static void |
smix(byte[] b,
int bi,
int r,
int n,
byte[] v,
byte[] xy) |
static boolean |
verifyHashFormat(String hash)
Returns true if the provided hash is a valid scrypt hash.
|
private static final org.slf4j.Logger logger
private static final int DEFAULT_SALT_LENGTH
private static final Pattern SCRYPT_PATTERN
public static String scrypt(String password, int n, int r, int p, int dkLen)
$s0$PARAMS$SALT$KEY
.
s0
identifies version 0 of the scrypt format, using a 128-bit salt and 256-bit derived key.
This method generates a 16 byte random salt internally.password
- passwordn
- CPU cost parameterr
- memory cost parameterp
- parallelization parameterdkLen
- the desired key length in bitspublic static String scrypt(String password, byte[] salt, int n, int r, int p, int dkLen)
scrypt(String, int, int, int, int)
.password
- passwordsalt
- the raw salt (16 bytes)n
- CPU cost parameterr
- memory cost parameterp
- parallelization parameterdkLen
- the desired key length in bitspublic static byte[] scrypt(byte[] input, byte[] salt, int n, int r, int p, int dkLen)
input
- input in bytessalt
- the raw salt (16 bytes)n
- CPU cost parameterr
- memory cost parameterp
- parallelization parameterdkLen
- the desired key length in bitspublic static String formatSalt(byte[] salt, int n, int r, int p)
private static String encodeParams(int n, int r, int p)
private static String formatHash(byte[] salt, int n, int r, int p, byte[] derived)
public static int calculateExpectedMemory(int n, int r, int p)
n
- the N value, iterations >= 2r
- the r value, block size >= 1p
- the p value, parallelization factor >= 1public static boolean check(String password, String hashed)
password
- plaintext passwordhashed
- scrypt hashed passwordpublic static boolean verifyHashFormat(String hash)
$s0$40801$ABCDEFGHIJKLMNOPQRSTUQ$hxU5g0eH6sRkBqcsiApI8jxvKRT+2QMCenV0GToiMQ8
Components:
s0 -- version. Currently only "s0" is supported
40801 -- hex-encoded N, r, p parameters. Scrypt#encodeParams()
for format
ABCDEFGHIJKLMNOPQRSTUQ -- Base64-encoded (URL-safe, no padding) salt value.
By default, 22 characters (16 bytes) but can be an arbitrary length between 11 and 64 characters (8 - 48 bytes) of random salt data
hxU5g0eH6sRkBqcsiApI8jxvKRT+2QMCenV0GToiMQ8 -- the Base64-encoded (URL-safe, no padding)
resulting hash component. By default, 43 characters (32 bytes) but can be an arbitrary length between 1 and MAX (depends on implementation, see RFC 7914)
hash
- the hash to verifyformatSalt(byte[], int, int, int)
public static List<Integer> parseParameters(String encodedParams)
encodedParams
- the String representation of the second section of the mcrypt format hashprivate static int log2(int n)
protected static byte[] deriveScryptKey(byte[] password, byte[] salt, int n, int r, int p, int dkLen) throws GeneralSecurityException
password
- passwordsalt
- saltn
- CPU cost parameterr
- memory cost parameterp
- parallelization parameterdkLen
- intended length of the derived key in bitsGeneralSecurityException
- when HMAC_SHA256 is not availableprivate static byte[] pbkdf2(String alg, byte[] p, byte[] s, int c, int dkLen) throws GeneralSecurityException
alg
- the HMAC algorithm to usep
- the passwords
- the saltc
- the iteration countdkLen
- the intended length, in octets, of the derived keyGeneralSecurityException
private static void pbkdf2(Mac mac, byte[] s, int c, byte[] dk, int dkLen) throws GeneralSecurityException
mac
- the pre-initialized Mac
instance to uses
- the saltc
- the iteration countdk
- the byte array that derived key will be placed indkLen
- the intended length, in octets, of the derived keyGeneralSecurityException
- if the key length is too longprivate static void smix(byte[] b, int bi, int r, int n, byte[] v, byte[] xy)
private static void blockmix_salsa8(byte[] by, int bi, int yi, int r)
private static int r(int a, int b)
private static void salsa20_8(byte[] b)
private static void blockxor(byte[] s, int si, byte[] d, int di, int len)
private static int integerify(byte[] b, int bi, int r)
public static int getDefaultSaltLength()
Copyright © 2021 Apache NiFi Project. All rights reserved.