public class Md5Crypt extends Object
Based on the public domain ("beer-ware") C implementation from Poul-Henning Kamp which was found at: crypt-md5.c @ freebsd.org
Source:
$FreeBSD: src/lib/libcrypt/crypt-md5.c,v 1.1 1999/01/21 13:50:09 brandon Exp $
Conversion to Kotlin and from there to Java in 2012.
The C style comments are from the original C code, the ones with "//" from the port.
This class is immutable and thread-safe.
Constructor and Description |
---|
Md5Crypt() |
Modifier and Type | Method and Description |
---|---|
static String |
apr1Crypt(byte[] keyBytes)
See
apr1Crypt(byte[], String) for details. |
static String |
apr1Crypt(byte[] keyBytes,
Random random)
See
apr1Crypt(byte[], String) for details. |
static String |
apr1Crypt(byte[] keyBytes,
String salt)
See
apr1Crypt(String, String) for details. |
static String |
apr1Crypt(String keyBytes)
See
apr1Crypt(String, String) for details. |
static String |
apr1Crypt(String keyBytes,
String salt)
Generates an Apache htpasswd compatible "$apr1$" MD5 based hash value.
|
static String |
md5Crypt(byte[] keyBytes)
Generates a libc6 crypt() compatible "$1$" hash value.
|
static String |
md5Crypt(byte[] keyBytes,
Random random)
Generates a libc6 crypt() compatible "$1$" hash value.
|
static String |
md5Crypt(byte[] keyBytes,
String salt)
Generates a libc crypt() compatible "$1$" MD5 based hash value.
|
static String |
md5Crypt(byte[] keyBytes,
String salt,
String prefix)
Generates a libc6 crypt() "$1$" or Apache htpasswd "$apr1$" hash value.
|
static String |
md5Crypt(byte[] keyBytes,
String salt,
String prefix,
Random random)
Generates a libc6 crypt() "$1$" or Apache htpasswd "$apr1$" hash value.
|
public static String apr1Crypt(byte[] keyBytes)
apr1Crypt(byte[], String)
for details.
A salt is generated for you using SecureRandom
; your own Random
in
apr1Crypt(byte[], Random)
.
keyBytes
- plaintext string to hash.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught. *apr1Crypt(byte[], String)
public static String apr1Crypt(byte[] keyBytes, Random random)
apr1Crypt(byte[], String)
for details.
A salt is generated for you using the user provided Random
.
keyBytes
- plaintext string to hash.random
- the instance of Random
to use for generating the salt. Consider using SecureRandom
or ThreadLocalRandom
.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught. *apr1Crypt(byte[], String)
public static String apr1Crypt(byte[] keyBytes, String salt)
apr1Crypt(String, String)
for details.
A salt is generated for you using SecureRandom
keyBytes
- plaintext string to hash.salt
- An APR1 salt. The salt may be null, in which case a salt is generated for you using
ThreadLocalRandom
; for more secure salts consider using SecureRandom
to generate your
own salts.IllegalArgumentException
- if the salt does not match the allowed patternIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.public static String apr1Crypt(String keyBytes)
apr1Crypt(String, String)
for details.
A salt is generated for you using ThreadLocalRandom
; for more secure salts consider using
SecureRandom
to generate your own salts and calling apr1Crypt(byte[], String)
.
keyBytes
- plaintext string to hash.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught.apr1Crypt(byte[], String)
public static String apr1Crypt(String keyBytes, String salt)
The algorithm is identical to the crypt(3) "$1$" one but produces different outputs due to the different salt prefix.
keyBytes
- plaintext string to hash.salt
- salt string including the prefix and optionally garbage at the end. The salt may be null, in which
case a salt is generated for you using ThreadLocalRandom
; for more secure salts consider using
SecureRandom
to generate your own salts.IllegalArgumentException
- if the salt does not match the allowed patternIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.public static String md5Crypt(byte[] keyBytes)
See md5Crypt(byte[], String)
for details.
A salt is generated for you using ThreadLocalRandom
; for more secure salts consider using
SecureRandom
to generate your own salts and calling md5Crypt(byte[], String)
.
keyBytes
- plaintext string to hash.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught.md5Crypt(byte[], String)
public static String md5Crypt(byte[] keyBytes, Random random)
See md5Crypt(byte[], String)
for details.
A salt is generated for you using the instance of Random
you supply.
keyBytes
- plaintext string to hash.random
- the instance of Random
to use for generating the salt. Consider using SecureRandom
or ThreadLocalRandom
.IllegalArgumentException
- when a NoSuchAlgorithmException
is caught.md5Crypt(byte[], String)
public static String md5Crypt(byte[] keyBytes, String salt)
See Crypt.crypt(String, String)
for details. We use SecureRandom
for seed generation by
default.
keyBytes
- plaintext string to hash.salt
- salt string including the prefix and optionally garbage at the end. The salt may be null, in which
case a salt is generated for you using ThreadLocalRandom
; for more secure salts consider using
SecureRandom
to generate your own salts.IllegalArgumentException
- if the salt does not match the allowed patternIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.public static String md5Crypt(byte[] keyBytes, String salt, String prefix)
See Crypt.crypt(String, String)
or apr1Crypt(String, String)
for details. We use
by default
.
keyBytes
- plaintext string to hash.salt
- real salt value without prefix or "rounds=". The salt may be null, in which case a salt
is generated for you using ThreadLocalRandom
; for more secure salts consider
using SecureRandom
to generate your own salts.prefix
- salt prefixIllegalArgumentException
- if the salt does not match the allowed patternIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.public static String md5Crypt(byte[] keyBytes, String salt, String prefix, Random random)
See Crypt.crypt(String, String)
or apr1Crypt(String, String)
for details.
keyBytes
- plaintext string to hash.salt
- real salt value without prefix or "rounds=". The salt may be null, in which case a salt
is generated for you using ThreadLocalRandom
; for more secure salts consider
using SecureRandom
to generate your own salts.prefix
- salt prefixrandom
- the instance of Random
to use for generating the salt. Consider using SecureRandom
or ThreadLocalRandom
.IllegalArgumentException
- if the salt does not match the allowed patternIllegalArgumentException
- when a NoSuchAlgorithmException
is caught.