In the previous article introduced the basic one-way encryption algorithm--MD5, also roughly said that it realizes the principle. This article continues the one-way encryption mentioned earlier, mainly about Sha, which, like MD5, is also a series, which includes several algorithms such as sha-1,sha-224,sha-256,sha-384, and SHA-512. Among them, sha-1,sha-224 and SHA-256 apply to messages that are not longer than 2^64 bits. SHA-384 and SHA-512 apply to messages that are not longer than 2^128 bits.
the basic concept of SHA algorithmSHA, all known as "secure Hash Algorithm", the Chinese name "Secure Hash Algorithm", is mainly applicable to the digital Signature algorithm (Digital Signature Standard DSS) defined in the digital signature (Digital Signature Algorithm DSA). For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest.
The idea of the algorithm is to receive a passage of plaintext and then convert it into a paragraph (usually smaller) cipher in an irreversible way, it can also be simply understood to take a string of input codes (called Pre-maps or information) and convert them to a shorter, fixed number of output sequences, which are hash values.
SHA-1 has two features: it is not possible to recover information from a message digest two different messages without generating the same message digest
the kind of the SHA algorithm
comparison of SHA-1 and MD5
Because both are derived by MD4, SHA-1 and MD5 are very similar to each other. Correspondingly, their strength and other characteristics are similar, but there are several differences:
Security for brute force attacks
The most significant and important difference is that the SHA-1 summary is 32 bits longer than the MD5 summary. The use of forced technology to produce any message to make its summary equal to the given report summary of the difficulty of MD5 is a 2^128 order of magnitude of operations, and SHA-1 is 2^160 order of magnitude of operations. In this way, SHA-1 has a greater strength for brute force attacks.
Security for password analysis
Because of the design of MD5, easy to be attacked by cryptanalysis, SHA-1 appears not easy to be attacked by this kind of attack.
Speed
On the same hardware, SHA-1 runs slower than MD5.
SHA Series algorithm programming using
SHA Series algorithm uses code
Import Java.security.MessageDigest; public class Shautils {/** * sha algorithm * @param data * @return/public static String Encryptsha (byte[] data)
{try {//judge the legality of the data if (data = = null) {throw new RuntimeException ("cannot be null");
}//Get the type of sha algorithm MessageDigest sha = messagedigest.getinstance ("SHA-512");
adding data sha.update;
Get message digest byte[] resultbytes = Sha.digest ();
Converts a byte array to a 16-binary String resultstring = Frombytestohex (resultbytes);
returns return resultstring;
catch (Exception e) {throw new RuntimeException (e); }/** * SHA algorithm * @param data * @return/public static string Encryptsha (String data) {try {//
To determine the legality of the data if (data = = null) {throw new RuntimeException ("cannot be null");
}//Get the type of sha algorithm MessageDigest sha = messagedigest.getinstance ("SHA-512");
Add Data Sha.update (Data.getbytes ());
Get message digest byte[] resultbytes = Sha.digest (); Converts a byte array to a 16-binary String resultstRing = Frombytestohex (resultbytes);
returns return resultstring;
catch (Exception e) {throw new RuntimeException (e); /** * Converts byte array to 16 * * @param resultbytes * @return/private static String Frombytestohex (byte[) resu
ltbytes) {StringBuilder builder = new StringBuilder ();
for (int i = 0; i < resultbytes.length i++) {if (Integer.tohexstring (0xFF & Resultbytes[i)). Length () = 1) {
Builder.append ("0"). Append (integer.tohexstring (0xFF & Resultbytes[i));
else {builder.append (integer.tohexstring (0xFF & Resultbytes[i]));
} return builder.tostring ();
}
}
Test code
public class Test {
//encrypted plaintext public
static final String DATA = "Test";
public static void Main (string[] args) throws Exception {
* * Test SHA *
/String Sharesult = Shautils.encryptsha (DA Ta.getbytes ());
System.out.println (DATA + ">>>SHA>>>" + Sharesult);
}