net.dongliu.commons.codec.Digests Maven / Gradle / Ivy
package net.dongliu.commons.codec;
import net.dongliu.commons.exception.Exceptions;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Utils for md5, sha, and other digest algorithm
*
* @author Dong Liu
*/
public class Digests {
private static final int BUFFER_SIZE = 8 * 1024;
//MessageDigest Algorithms supported by jdk
public static final String MD2 = "MD2";
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
public static final String SHA256 = "SHA-256";
public static final String SHA384 = "SHA-384";
public static final String SHA512 = "SHA-512";
/**
* Create new digest with algorithm
*/
public static Digest create(String algorithm) {
MessageDigest md;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw Exceptions.sneakyThrow(e);
}
return new Digest(md);
}
/**
* Create md2 digest
*/
public static Digest md2() {
return create(MD2);
}
/**
* Create md5 digest
*/
public static Digest md5() {
return create(MD5);
}
/**
* Create md5 digest with data
*/
public static Digest md5(byte[] data) {
return create(MD5).update(data);
}
/**
* Create sha128 digest
*/
public static Digest sha1() {
return create(SHA1);
}
/**
* Create sha128 digest with data
*/
public static Digest sha1(byte[] data) {
return create(SHA1).update(data);
}
/**
* Create sha256 digest
*/
public static Digest sha256() {
return create(SHA256);
}
/**
* Create sha256 digest with data
*/
public static Digest sha256(byte[] data) {
return create(SHA256).update(data);
}
/**
* Create sha384 digest
*/
public static Digest sha384() {
return create(SHA384);
}
/**
* Create sha384 digest with data
*/
public static Digest sha384(byte[] data) {
return create(SHA384).update(data);
}
/**
* Create sha512 digest
*/
public static Digest sha512() {
return create(SHA512);
}
/**
* Create sha512 digest with data
*/
public static Digest sha512(byte[] data) {
return create(SHA512).update(data);
}
public static class Digest {
/**
* Digester with specified algorithm
*/
private final MessageDigest md;
private Digest(MessageDigest md) {
this.md = md;
}
/**
* add byte date
*/
public Digest update(byte[] data) {
md.update(data);
return this;
}
/**
* add byte date
*/
public Digest update(byte[] data, int offset, int len) {
md.update(data, offset, len);
return this;
}
/**
* Digest data from byte buffer
*/
public Digest update(ByteBuffer buffer) {
md.update(buffer);
return this;
}
/**
* Digest data from char buffer
*/
public Digest update(CharBuffer buffer, Charset charset) {
int len = buffer.remaining();
int chunkSize = 1024;
for (int offset = 0; offset < len; offset += chunkSize) {
CharBuffer subBuffer = buffer.subSequence(offset, Math.min(offset + chunkSize, len));
ByteBuffer byteBuffer = charset.encode(subBuffer);
md.update(byteBuffer);
}
return this;
}
/**
* Digest data from char buffer
*/
public Digest update(CharBuffer buffer) {
return update(buffer, StandardCharsets.UTF_8);
}
/**
* Digest data from char sequence
*/
public Digest update(CharSequence charSequence, Charset charset) {
CharBuffer buffer = CharBuffer.wrap(charSequence);
return update(buffer, charset);
}
/**
* Digest data from char sequence
*/
public Digest update(CharSequence charSequence) {
return update(charSequence, StandardCharsets.UTF_8);
}
/**
* Digest data from input stream
*/
public Digest update(InputStream in) {
try (InputStream input = in) {
byte[] buffer = new byte[BUFFER_SIZE];
int count;
while ((count = input.read(buffer)) != -1) {
md.update(buffer, 0, count);
}
} catch (IOException e) {
throw Exceptions.sneakyThrow(e);
}
return this;
}
/**
* Get digest result as byte array
*/
public byte[] toByteArray() {
return md.digest();
}
/**
* Get digest result as hex string upper case
*/
public String toHexUpper() {
return Hexes.hexUpper(toByteArray());
}
/**
* Get digest result as hex string lower case
*/
public String toHexLower() {
return Hexes.hexLower(toByteArray());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy