Class Hash
java.lang.Object
com.codename1.security.Hash
Streaming and one-shot cryptographic hash (message digest) functions. The supported algorithms are exposed as constants on this class:
MD5-- 128 bit, legacy interop only (broken collision resistance)SHA1-- 160 bit, legacy interop only (broken collision resistance)SHA224-- 224 bit (SHA-2 family)SHA256-- 256 bit (SHA-2 family, recommended general-purpose hash)SHA384-- 384 bit (SHA-2 family)SHA512-- 512 bit (SHA-2 family)
Quick example
byte[] digest = Hash.sha256("hello".getBytes("UTF-8"));
String hex = Hash.toHex(digest);
// streaming
Hash h = Hash.create(Hash.SHA256);
h.update("hello".getBytes("UTF-8"));
h.update(" world".getBytes("UTF-8"));
byte[] out = h.digest();
The implementations are written entirely in portable Java so they are available on every supported platform. They produce identical output to the equivalent algorithm in the standard JDK.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMD5 algorithm identifier (128-bit digest).static final StringSHA-1 algorithm identifier (160-bit digest).static final StringSHA-224 algorithm identifier (224-bit digest, SHA-2 family).static final StringSHA-256 algorithm identifier (256-bit digest, SHA-2 family).static final StringSHA-384 algorithm identifier (384-bit digest, SHA-2 family).static final StringSHA-512 algorithm identifier (512-bit digest, SHA-2 family). -
Method Summary
Modifier and TypeMethodDescriptionstatic HashCreates a streaming hash for the given algorithm.byte[]digest()Finalises the running hash and returns the digest.byte[]digest(byte[] data) Convenience: feeddatathen return the digest.intNumber of bytes in the digest produced by this hash.static byte[]Decodes a hex string back into bytes.static byte[]md5(byte[] data) One-shot MD5 hash.voidreset()Resets the running digest so the instance can be reused.static byte[]sha1(byte[] data) One-shot SHA-1 hash.static byte[]sha224(byte[] data) One-shot SHA-224 hash.static byte[]sha256(byte[] data) One-shot SHA-256 hash (recommended general-purpose hash).static byte[]sha384(byte[] data) One-shot SHA-384 hash.static byte[]sha512(byte[] data) One-shot SHA-512 hash.static StringtoHex(byte[] data) Encodes the bytes as a lowercase hex string (two characters per byte).voidupdate(byte b) Feeds a single byte into the running hash.voidupdate(byte[] data) Feeds the entire array into the running hash.voidupdate(byte[] data, int offset, int length) Feeds a slice of the array into the running hash.
-
Field Details
-
MD5
MD5 algorithm identifier (128-bit digest). Provided for legacy interop -- MD5 is no longer considered collision resistant and should not be used for new security-sensitive code.- See Also:
-
SHA1
SHA-1 algorithm identifier (160-bit digest). Provided for legacy interop -- SHA-1 is no longer considered collision resistant and should not be used for new security-sensitive code.- See Also:
-
SHA224
SHA-224 algorithm identifier (224-bit digest, SHA-2 family).- See Also:
-
SHA256
SHA-256 algorithm identifier (256-bit digest, SHA-2 family). Recommended default for general-purpose hashing.- See Also:
-
SHA384
SHA-384 algorithm identifier (384-bit digest, SHA-2 family).- See Also:
-
SHA512
SHA-512 algorithm identifier (512-bit digest, SHA-2 family).- See Also:
-
-
Method Details
-
create
-
update
public void update(byte[] data) Feeds the entire array into the running hash.
Parameters
data: bytes to append to the running digest
-
update
public void update(byte[] data, int offset, int length) Feeds a slice of the array into the running hash.
Parameters
-
data: bytes to append to the running digest -
offset: index of the first byte to read -
length: number of bytes to read
-
-
update
public void update(byte b) Feeds a single byte into the running hash. -
digest
public byte[] digest()Finalises the running hash and returns the digest. The hash is reset after this call so the same instance may be reused for another message.
Returns
the raw digest bytes (algorithm specific length)
-
digest
public byte[] digest(byte[] data) Convenience: feeddatathen return the digest. -
digestLength
public int digestLength()Number of bytes in the digest produced by this hash. -
reset
public void reset()Resets the running digest so the instance can be reused. -
md5
public static byte[] md5(byte[] data) One-shot MD5 hash. -
sha1
public static byte[] sha1(byte[] data) One-shot SHA-1 hash. -
sha224
public static byte[] sha224(byte[] data) One-shot SHA-224 hash. -
sha256
public static byte[] sha256(byte[] data) One-shot SHA-256 hash (recommended general-purpose hash). -
sha384
public static byte[] sha384(byte[] data) One-shot SHA-384 hash. -
sha512
public static byte[] sha512(byte[] data) One-shot SHA-512 hash. -
toHex
Encodes the bytes as a lowercase hex string (two characters per byte). -
fromHex
Decodes a hex string back into bytes. The string must contain an even number of hex characters (whitespace and the0xprefix are not stripped -- pass cleaned input).
-