- Message digest in java
- Constructor Summary
- Method Summary
- Methods declared in class java.security.MessageDigestSpi
- Methods declared in class java.lang.Object
- Constructor Detail
- MessageDigest
- Method Detail
- getInstance
- getInstance
- getInstance
- getProvider
- update
- update
- update
- update
- digest
- digest
- digest
- toString
- isEqual
- reset
- getAlgorithm
- getDigestLength
- clone
- Class MessageDigest
- Message digest in java
- Constructor Summary
- Method Summary
- Methods inherited from class java.security.MessageDigestSpi
- Methods inherited from class java.lang.Object
- Constructor Detail
- MessageDigest
- Method Detail
- getInstance
- getInstance
- getInstance
- getProvider
- update
- update
- update
- update
- digest
- digest
- digest
- toString
- isEqual
- reset
- getAlgorithm
- getDigestLength
- clone
- Class MessageDigest
Message digest in java
This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation. The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state. Implementations are free to implement the Cloneable interface. Client applications can test cloneability by attempting cloning and catching the CloneNotSupportedException:
MessageDigest md = MessageDigest.getInstance("SHA-256"); try < md.update(toChapter1); MessageDigest tc1 = md.clone(); byte[] toChapter1Digest = tc1.digest(); md.update(toChapter2); . etc. >catch (CloneNotSupportedException cnse)
Constructor Summary
Method Summary
Performs a final update on the digest using the specified array of bytes, then completes the digest computation.
Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.
Methods declared in class java.security.MessageDigestSpi
Methods declared in class java.lang.Object
Constructor Detail
MessageDigest
Method Detail
getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
Returns a MessageDigest object that implements the specified digest algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the first Provider that supports the specified algorithm is returned. Note that the list of registered providers may be retrieved via the Security.getProviders() method.
getInstance
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Returns a MessageDigest object that implements the specified digest algorithm. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list. Note that the list of registered providers may be retrieved via the Security.getProviders() method.
getInstance
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Returns a MessageDigest object that implements the specified digest algorithm. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.
getProvider
update
public void update(byte input)
update
public void update(byte[] input, int offset, int len)
update
public void update(byte[] input)
update
Update the digest using the specified ByteBuffer. The digest is updated using the input.remaining() bytes starting at input.position() . Upon return, the buffer’s position will be equal to its limit; its limit will not have changed.
digest
Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.
digest
Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.
digest
public byte[] digest(byte[] input)
Performs a final update on the digest using the specified array of bytes, then completes the digest computation. That is, this method first calls update(input) , passing the input array to the update method, then calls digest() .
toString
isEqual
public static boolean isEqual(byte[] digesta, byte[] digestb)
Compares two digests for equality. Two digests are equal if they have the same length and all bytes at corresponding positions are equal.
reset
getAlgorithm
Returns a string that identifies the algorithm, independent of implementation details. The name should be a standard Java Security name (such as «SHA-256»). See the MessageDigest section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names.
getDigestLength
public final int getDigestLength()
Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.
clone
public Object clone() throws CloneNotSupportedException
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Class MessageDigest
This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation.
The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.
Implementations are free to implement the Cloneable interface. Client applications can test cloneability by attempting cloning and catching the CloneNotSupportedException :
MessageDigest md = MessageDigest.getInstance("SHA-256"); try < md.update(toChapter1); MessageDigest tc1 = md.clone(); byte[] toChapter1Digest = tc1.digest(); md.update(toChapter2); . etc. >catch (CloneNotSupportedException cnse)
Note that if a given implementation is not cloneable, it is still possible to compute intermediate digests by instantiating several instances, if the number of digests is known in advance.
Note that this class is abstract and extends from MessageDigestSpi for historical reasons. Application developers should only take notice of the methods defined in this MessageDigest class; all the methods in the superclass are intended for cryptographic service providers who wish to supply their own implementations of message digest algorithms.
Message digest in java
This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value. A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation. The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state. Implementations are free to implement the Cloneable interface. Client applications can test cloneability by attempting cloning and catching the CloneNotSupportedException:
MessageDigest md = MessageDigest.getInstance("SHA-256"); try < md.update(toChapter1); MessageDigest tc1 = md.clone(); byte[] toChapter1Digest = tc1.digest(); md.update(toChapter2); . etc. >catch (CloneNotSupportedException cnse)
Constructor Summary
Method Summary
Performs a final update on the digest using the specified array of bytes, then completes the digest computation.
Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.
Methods inherited from class java.security.MessageDigestSpi
Methods inherited from class java.lang.Object
Constructor Detail
MessageDigest
Method Detail
getInstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
Returns a MessageDigest object that implements the specified digest algorithm. This method traverses the list of registered security Providers, starting with the most preferred Provider. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the first Provider that supports the specified algorithm is returned. Note that the list of registered providers may be retrieved via the Security.getProviders() method.
getInstance
public static MessageDigest getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
Returns a MessageDigest object that implements the specified digest algorithm. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified provider is returned. The specified provider must be registered in the security provider list. Note that the list of registered providers may be retrieved via the Security.getProviders() method.
getInstance
public static MessageDigest getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
Returns a MessageDigest object that implements the specified digest algorithm. A new MessageDigest object encapsulating the MessageDigestSpi implementation from the specified Provider object is returned. Note that the specified Provider object does not have to be registered in the provider list.
getProvider
update
public void update(byte input)
update
public void update(byte[] input, int offset, int len)
update
public void update(byte[] input)
update
Update the digest using the specified ByteBuffer. The digest is updated using the input.remaining() bytes starting at input.position() . Upon return, the buffer’s position will be equal to its limit; its limit will not have changed.
digest
Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.
digest
Completes the hash computation by performing final operations such as padding. The digest is reset after this call is made.
digest
public byte[] digest(byte[] input)
Performs a final update on the digest using the specified array of bytes, then completes the digest computation. That is, this method first calls update(input) , passing the input array to the update method, then calls digest() .
toString
isEqual
public static boolean isEqual(byte[] digesta, byte[] digestb)
reset
getAlgorithm
Returns a string that identifies the algorithm, independent of implementation details. The name should be a standard Java Security name (such as «SHA-256»). See the MessageDigest section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard algorithm names.
getDigestLength
public final int getDigestLength()
Returns the length of the digest in bytes, or 0 if this operation is not supported by the provider and the implementation is not cloneable.
clone
public Object clone() throws CloneNotSupportedException
Class MessageDigest
This MessageDigest class provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256. Message digests are secure one-way hash functions that take arbitrary-sized data and output a fixed-length hash value.
A MessageDigest object starts out initialized. The data is processed through it using the update methods. At any point reset can be called to reset the digest. Once all the data to be updated has been updated, one of the digest methods should be called to complete the hash computation.
The digest method can be called once for a given number of updates. After digest has been called, the MessageDigest object is reset to its initialized state.
Implementations are free to implement the Cloneable interface. Client applications can test cloneability by attempting cloning and catching the CloneNotSupportedException:
MessageDigest md = MessageDigest.getInstance("SHA-256"); try < md.update(toChapter1); MessageDigest tc1 = md.clone(); byte[] toChapter1Digest = tc1.digest(); md.update(toChapter2); . etc. >catch (CloneNotSupportedException cnse)
Note that if a given implementation is not cloneable, it is still possible to compute intermediate digests by instantiating several instances, if the number of digests is known in advance.
Note that this class is abstract and extends from MessageDigestSpi for historical reasons. Application developers should only take notice of the methods defined in this MessageDigest class; all the methods in the superclass are intended for cryptographic service providers who wish to supply their own implementations of message digest algorithms.