All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.libg.cryptography.Digest Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.libg.cryptography;

import java.util.Arrays;

import aQute.lib.hex.Hex;

public abstract class Digest {
	final byte[] digest;

	protected Digest(byte[] checksum, int width) {
		this.digest = checksum;
		if (digest.length != width)
			throw new IllegalArgumentException("Invalid width for digest: " + digest.length + " expected " + width);
	}

	public byte[] digest() {
		return digest;
	}

	public String asHex() {
		return Hex.toHexString(digest());
	}

	@Override
	public String toString() {
		return String.format("%s(d=%s)", getAlgorithm(), Hex.toHexString(digest));
	}

	public abstract String getAlgorithm();

	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Digest))
			return false;

		Digest d = (Digest) other;
		return Arrays.equals(d.digest, digest);
	}

	@Override
	public int hashCode() {
		return Arrays.hashCode(digest);
	}

	public byte[] toByteArray() {
		return digest();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy