7, SHA1 encryption algorithm __SHA1

Source: Internet
Author: User
Tags sha1 sha1 encryption

SHA1 Encryption Algorithm

SHA is a data encryption algorithm, which has been developed and improved by encryption experts for many years and has become one of the most secure hashing algorithms, and is widely used. The idea of the algorithm is to receive a clear text and then convert it into a paragraph (usually smaller) cipher in an irreversible way, or simply to take a string of input codes (called Pre-maps or information) and convert them to a shorter, The process of a fixed number of digits in the output sequence that is the hash value (also known as Information Digest or information authentication code). The hash function value can be said to be a "fingerprint" or "digest" of the plaintext, so a digital signature on the hash value can be considered a digital signature on the plaintext.

Secure Hash algorithm SHA (Secure hash Algorithm,sha) is a national standard FIPS pub 180 issued by the National Institute of Standards and Technology, the latest standard has been updated in 2008 to FIPS pub 180-3. It prescribes the sha-1,sha-224,sha-256,sha-384, and SHA-512 these one-way hashing algorithms. 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.

algorithm principle

SHA-1 is a data encryption algorithm, the idea of the algorithm is to receive a clear text and then convert it into a paragraph (usually smaller) cipher in an irreversible way, or simply to take a string of input codes (called Pre-maps or information) and convert them to a shorter, The process of a fixed number of digits in the output sequence that is the hash value (also known as Information Digest or information authentication code).

The safety of one-way hash function is that the operation process of producing hash value has a strong one-way. If the password is embedded in the input sequence, then no one can produce the correct hash value without knowing the password, thus ensuring its security. The SHA blocks the input stream by 512 bits (64 bytes) per block and produces 20 bytes of output called the Information authentication Code or information digest.

This algorithm input the length of the message is not limited, produces the output is a 160-bit message digest. The input is processed by a 512-bit grouping. SHA-1 is irreversible, conflict-proof, and has a good avalanche effect.

Through hashing algorithm can realize digital signature, the principle of digital signature is to transmit the clear text through a function operation (Hash) conversion to the newspaper digest (different clear text corresponding to different message digest), the newspaper digest to be encrypted with plaintext sent to the receiver, The recipient will accept the clear text to produce a new digest to be sent to the sender of the digest to decrypt the comparison, the results of the comparison are consistent to indicate that the plaintext has not been altered, if it is inconsistent that the plaintext has been tampered with.

algorithm Java Implementation

public class Securitysha1utils {/** * @Comment SHA1 Implementation * @Author Ron * @Date September 13, 2017 3:30:36
        * @return */public static string Shaencode (String inStr) throws Exception {MessageDigest sha = null;
        try {sha = Messagedigest.getinstance ("Sha");
            catch (Exception e) {System.out.println (e.tostring ());
            E.printstacktrace ();
        Return "";
        } byte[] ByteArray = instr.getbytes ("UTF-8");
        byte[] md5bytes = Sha.digest (ByteArray);
        StringBuffer hexvalue = new StringBuffer ();
            for (int i = 0; i < md5bytes.length i++) {int val = ((int) md5bytes[i]) & 0xFF;
            if (Val <) {Hexvalue.append ("0");
        } hexvalue.append (Integer.tohexstring (Val));
    return hexvalue.tostring (); public static void Main (String args[]) throws Exception {string str = new String("amigoxiexiexingxing");
        System.out.println ("Original:" + str);
    System.out.println ("After Sha:" + shaencode (str)); }
}

But in the actual application, we can directly introduce org.apache.commons.codec.digest.DigestUtils, and then follow the following method to invoke encryption.

/**
* @Comment SHA1 encryption Password
* @Author Ron
* @Date September 12, 2017 pm 2:46:31
* @return
/public static String Encodepassword (String psw) {
    if (Stringutils.isempty (PSW)) {return
        null;
    } else{return
        Digestutils.sha1hex (PSW);
    }

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.