Document directory
- Java. Security. messagedigest
Java encryption technology: Message Digest.
A message digest is a digital fingerprint of a data block. Calculate a data block of any length and generate a unique fingerprint (for sha1, a 20-byte binary array ).
The message digest has two basic attributes:
It is difficult for two different packets to generate the same abstract.
It is difficult to generate a message for a specified digest, and the specified digest is calculated by the corresponding digest.
Representatives: sha1 of the National Institute of Standards and Technology and MD5 proposed by Ronald Rivest of the Massachusetts Institute of Technology
Java. Security. messagedigest
java.lang.Object | +----java.security.MessageDigest
-
Public abstract class
Messagedigest
-
Extends object
Messagedigest provides message digest algorithms, such as MD5 or Sha. A message digest is a secure one-way hash function that uses data of any size and outputs a fixed-length hash value.
Like other algorithm-based classes in Java security, messagedigest has two main components:
-
Message Digest API(Application Interface)
-
This is the interface of the method called by the application of the message digest service. This API is composed of all public methods.
-
Message Digest SPI(Service Provider Interface)
-
This interface is implemented by the provider that provides special algorithms. It is prefixed by all names
Engine. Each such method is called by a public API method with the corresponding name. For example,
engineReset
Method
reset
Method call. The SPI method is abstract; the provider must provide a specific implementation.
The messagedigest object is initialized at startup. Use the update method to process data. The reset summary can be called anywhere. Once all the data to be modified is modified, a digest method is called to calculate the hash code.
You can only calldigest
Method once. Before callingdigest
Then, the messagedigest object is reset to the initial state.
You can freely implement the cloneable interface.instanceof Cloneable
Test reproducibility:
MessageDigest md = MessageDigest.getInstance("SHA"); if (md instanceof Cloneable) { md.update(toChapter1); MessageDigest tc1 = md.clone(); byte[] toChapter1Digest = tc1.digest; md.update(toChapter2); ...etc. } else { throw new DigestException("couldn't make digest of partial content"); }
Note: If the given implementation is non-reproducible and the number of summaries is known in advance, several instances can still be used as examples to calculate the intermediate summary.
Constructor
Messagedigest
protected MessageDigest(String algorithm)
-
Creates a message digest using the specified algorithm name.
-
-
Parameters:
-
Algorithm-standard string name of the digest algorithm. Appendix A of the API specification for the Java password structure. -->
Method
Getinstance
public static MessageDigest getInstance(String algorithm) throws NoSuchAlgorithmException
-
Generates a messagedigest object that implements the specified Digest algorithm. If the default provider package contains a subclass that implements the messagedigest algorithm, an instance of this subclass is returned. If the algorithm is unavailable in the default package, other packages will be searched.
-
-
Parameters:
-
Algorithm-name of the applied algorithm. Appendix A of the API specification for the Java password structure. -->
-
Return Value:
-
A message digest object that implements the specified algorithm.
-
Throw:Nosuchalgorithmexception
-
If the algorithm is unavailable in the caller environment.
Getinstance
public static MessageDigest getInstance(String algorithm,String provider) throwsNoSuchAlgorithmException,NoSuchProviderException
-
Generate a messagedigest object to implement the specified algorithm. If the provider's algorithm is available, the algorithm is provided by the provider.
-
-
Parameters:
-
Algorithm-name of the applied algorithm. Appendix A of the API specification for the Java password structure. -->
-
Provider-provider name.
-
Return Value:
-
A message digest object that implements the specified algorithm.
-
Throw:Nosuchalgorithmexception
-
If the algorithm is unavailable in the package provided by the caller.
-
Throw:Nosuchproviderexception
-
If the provider is unavailable in the environment.
-
See:
-
Provider
Update
public void update(byte input)
-
Modify the Abstract With the specified byte.
-
-
Parameters:
-
Input-used to modify the bytes of the abstract.
Update
public void update(byte input[], int offset, int len)
-
Modify the Abstract With the specified byte array starting from the offset specified by the array.
-
-
Parameters:
-
Input-this byte array.
-
Offset-the offset starting from the byte array.
-
Len-slave
offset
The number of bytes.
Update
public void update(byte input[])
-
Modify the Abstract With the specified byte array.
-
-
Parameters:
-
Input-this byte array.
Digest
public byte[] digest()
-
The hash code is calculated by executing the final operations such as filling. Reset the summary after the call.
-
-
Return Value:
-
The byte array that stores the hash value of the result.
Digest
public byte[] digest(byte input[])
-
Use the specified byte array to perform the last modification to the abstract, and then complete the summary calculation. That is, This method first calls update on the array and then calls Digest ().
-
-
Parameters:
-
Input-the input value used for modification before the summary calculation is complete.
-
Return Value:
-
The byte array of the result hash value.
Tostring
public String toString()
-
Returns the string representation of the message digest object.
-
-
Overwrite:
-
Tostring in Class Object
Isequal
public static boolean isEqual(byte digesta[], byte digestb[])
-
Compare whether the two summaries are the same. For a simple comparison.
-
-
Parameters:
-
Digesta-a summary to be compared.
-
Digestb-another abstract to be compared.
-
Return Value:
-
True if the two summaries are equal; otherwise, false.
Reset
public void reset()
-
Reset the summary for future use.
A message digest is a digital fingerprint of a data block. Calculate a data block of any length and generate a unique fingerprint (for sha1, a 20-byte binary array ).
The message digest has two basic attributes:
It is difficult for two different packets to generate the same abstract.
It is difficult to generate a message for a specified digest, and the specified digest is calculated by the corresponding digest.
Representatives: sha1 of the National Institute of Standards and Technology and MD5.
Message Digest MD5 and Sha usage
Usage:
First, generate a messagedigest class to determine the calculation method.
Java. Security. messagedigest alga = java. Security. messagedigest. getinstance ("SHA-1 ");
Add the information of the digest to be calculated
Alga. Update (myinfo. getbytes ());
Calculate the abstract
Byte [] digesta = alga. Digest ();
Your information and summary sent to others
Other people initialize with the same method, add information, and finally compare the summary.
Algb. isequal (digesta, algb. Digest ())
Related AIP
Java. Security. messagedigest class
Static getinstance (string algorithm)
Returns a messagedigest object that implements the specified algorithm.
Parameter: algorithm name, such as SHA-1 or MD5
Void Update (byte input)
Void Update (byte [] input)
Void Update (byte [] input, int offset, int Len)
Add the information of the digest to be calculated
Byte [] Digest ()
After the calculation is completed, return the computed Summary (for MD5, 16 bits, and Sha, 20 bits)
Void reset ()
Reset
Static Boolean isequal (byte [] digesta, byte [] digestb)
Whether the two abstracts are the same
Code:
Import java. Security .*;
Public class mydigest {
Public static void main (string [] ARGs ){
Mydigest my = new mydigest ();
My. testdigest ();
}
Public void testdigest (){
Try {
String myinfo = "my test information ";
// Java. Security. messagedigest
// ALG = java. Security. messagedigest. getinstance ("MD5 ");
Java. Security. messagedigest alga = java. Security. messagedigest
. Getinstance ("SHA-1 ");
Alga. Update (myinfo. getbytes ());
Byte [] digesta = alga. Digest ();
System. Out. println ("This information abstract is:" + byte2hex (digesta ));
// You can pass your information (myinfo) and digest (digesta) to other people in a certain way to determine whether the information is changed or transmitted normally.
Java. Security. messagedigest algb = java. Security. messagedigest
. Getinstance ("SHA-1 ");
Algb. Update (myinfo. getbytes ());
If (algb. isequal (digesta, algb. Digest ())){
System. Out. println ("information check is normal ");
} Else {
System. Out. println ("different abstract ");
}
} Catch (Java. Security. nosuchalgorithmexception ex ){
System. Out. println ("invalid Digest algorithm ");
}
}
Public String byte2hex (byte [] B) // two-line conversion string
{
String HS = "";
String stmp = "";
For (INT n = 0; n <B. length; n ++ ){
Stmp = (Java. Lang. Integer. tohexstring (B [N] & 0xff ));
If (stmp. Length () = 1)
HS = HS + "0" + stmp;
Else
HS = HS + stmp;
}
Return HS. touppercase ();
}
}
B [N] & 0xff converts byte to int.
Because 0xff is an integer, byte [] B; B [Index] & 0xff is closer to the big data type, which is an integer.
The Byte in Java is sign, so the forced conversion of a negative byte to an int will damage the original binary representation, for example:
Byte BB = (byte) 0xf1; // 11110001
Printbinary (INT) bb); // 11111111111111111111111111110001
Printbinary (BB & 0xff); // 00000000000000000000000011110001
Running result:
INT:-15 binary:
11111111111111111111111111110001
INT: 241 binary:
00000000000000000000000011110001