Functions and usage of messagedigest

Source: Internet
Author: User

The messagedigest class provides information digest algorithms for applications, such as MD5 or Sha algorithms. Information digest is a secure one-way hash function that receives data of any size and outputs a hash value of a fixed length.
The messagedigest object is initialized. This object uses the update () method to process data. You can call the reset () method to reset the Summary at any time. Once all the data to be updated has been updated, call one of the Digest () Methods to complete hash calculation.

The Digest method can only be called once for a specified number of updated data. After digest is called, The messagedigest object is reset to its initial state.

1. Public static messagedigest getinstance (string algorithm)
Throws nosuchalgorithmexception returns the messagedigest object that implements the specified Digest algorithm. Algorithm-name of the requested algorithm 2. Public static messagedigest getinstance (string algorithm,
String provider)
Throws nosuchalgorithmexception,
Nosuchproviderexception returns the messagedigest object that implements the specified Digest algorithm. Algorithm-name of the requested algorithm provider-provider. 3. Public void Update (byte [] input) uses the specified byte array to update the abstract. 4. Public byte [] Digest () completes hash calculation by performing final operations such as filling. After this method is called, the summary is reset. 5. Public static Boolean isequal (byte [] digesta,
Byte [] digestb)
Compare the equality of the two summaries. Perform simple byte comparison. Note: The provider can use Java. security. Security. getproviders ()
Method to obtain the list of registered providers. Commonly used "sun" provided by Sun commonly used algorithm names are: md2 MD5 SHA-1 SHA-256 SHA-384 SHA-512 code example: 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 ));
// Send your information (myinfo) and digest (digesta) to other users 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;
If (n <B. Length-1) HS = HS + ":";
}
Return HS. touppercase ();
}
}

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ma1kong/archive/2008/07/16/2662997.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.