/**
* Converts a string into a MD4 hash.
*
* @param original the original string to be encrypted.
* @return the returned hash as bytes.
*/
public static String hashMD4(String original) {
String copy = "";
String pwd = original;
if (pwd == null) {
pwd = "";
}
try {
MD4Digest md4 = new MD4Digest();
byte[] pwdBytes = original.getBytes();
md4.update(pwdBytes, 0, pwdBytes.length);
byte[] encPwd = new byte[md4.getDigestSize()];
md4.doFinal(encPwd, 0);
copy = getHex(encPwd).toLowerCase();
// new BigInteger(1, encPwd).toString(16);
} catch (Throwable nsae) {
log.error(nsae.getMessage());
}
return copy;
}
/**
* Converts a string into a MD4 hash suitable for the NTLM v1 authentication
*
* @param original the original string
*
* @return the MD4 hash
*/
public static String hashNTLM1(String original) {
try {
if (original == null) {
original = "";
}
MD4Digest md4 = new MD4Digest();
int len = original.length();
byte[] pwdBytes = new byte[len * 2];
for (int i = 0; i < len; i++) {
char ch = original.charAt(i);
pwdBytes[i * 2] = (byte) ch;
pwdBytes[i * 2 + 1] = (byte) ((ch >> 8) & 0xFF);
}
md4.update(pwdBytes, 0, pwdBytes.length);
byte[] encPwd = new byte[16];
md4.doFinal(encPwd, 0);
return CryptUtil.getHex(encPwd).substring(0, 32);
} catch (Throwable nsae) {
log.error(nsae.getMessage());
return null;
}
}
public static void bcMD4()
{
MD4Digest digest = new MD4Digest();
digest.update(src.getBytes(),0,src.getBytes().length);
byte[] md4Bytes = new byte[digest.getDigestSize()];
digest.doFinal(md4Bytes, 0);
System.out.println("bouncy castle MD4:" + org.bouncycastle.util.encoders.Hex.toHexString(md4Bytes));
}
public static final void register(final Map<String, Hasher> hashers) {
hashers.put(BouncyCastleHasher.GOST,
new BouncyCastleHasher(new GOST3411Digest()));
hashers.put(BouncyCastleHasher.MD2,
new BouncyCastleHasher(new MD2Digest()));
hashers.put(BouncyCastleHasher.MD4,
new BouncyCastleHasher(new MD4Digest()));
hashers.put(BouncyCastleHasher.MD5,
new BouncyCastleHasher(new MD5Digest()));
hashers.put(BouncyCastleHasher.RIPEMD128,
new BouncyCastleHasher(new RIPEMD128Digest()));
hashers.put(BouncyCastleHasher.RIPEMD160,
new BouncyCastleHasher(new RIPEMD160Digest()));
hashers.put(BouncyCastleHasher.RIPEMD256,
new BouncyCastleHasher(new RIPEMD256Digest()));
hashers.put(BouncyCastleHasher.RIPEMD320,
new BouncyCastleHasher(new RIPEMD320Digest()));
hashers.put(BouncyCastleHasher.SHA1,
new BouncyCastleHasher(new SHA1Digest()));
hashers.put(BouncyCastleHasher.SHA224,
new BouncyCastleHasher(new SHA224Digest()));
hashers.put(BouncyCastleHasher.SHA256,
new BouncyCastleHasher(new SHA256Digest()));
hashers.put(BouncyCastleHasher.SHA3,
new BouncyCastleHasher(new SHA3Digest()));
hashers.put(BouncyCastleHasher.SHA384,
new BouncyCastleHasher(new SHA384Digest()));
hashers.put(BouncyCastleHasher.SHA512,
new BouncyCastleHasher(new SHA512Digest()));
hashers.put(BouncyCastleHasher.SHA512_T,
new BouncyCastleHasher(new SHA512tDigest(7 * 8)));
hashers.put(BouncyCastleHasher.SKEIN1024, new BouncyCastleHasher(
new SkeinDigest(SkeinDigest.SKEIN_1024, Long.BYTES * 8)));
hashers.put(BouncyCastleHasher.SKEIN256, new BouncyCastleHasher(
new SkeinDigest(SkeinDigest.SKEIN_256, Long.BYTES * 8)));
hashers.put(BouncyCastleHasher.SKEIN512, new BouncyCastleHasher(
new SkeinDigest(SkeinDigest.SKEIN_512, Long.BYTES * 8)));
hashers.put(BouncyCastleHasher.SM3,
new BouncyCastleHasher(new SM3Digest()));
hashers.put(BouncyCastleHasher.TIGER,
new BouncyCastleHasher(new TigerDigest()));
hashers.put(BouncyCastleHasher.WHIRLPOOL2,
new BouncyCastleHasher(new WhirlpoolDigest()));
}