The SHA secure hashing algorithm for Android data encryption _android

Source: Internet
Author: User
Tags md5 md5 encryption

Objective:

For the SHA Safe hashing algorithm, before how to use, just stay in the heard of the stage, today in the picture Caching framework Glide source code found that its cache key is not MD5 encryption algorithm, but SHA-256 encryption algorithm, this just reminded me of curiosity, so take advantage of the evening nothing, To learn.

Several other encryption methods:

RSA encryption of Android data encryption
AES encryption of Android data encryption
Des encryption of Android data encryption
MD5 encryption of Android data encryption
BASE64 coding algorithm of Android data encryption

SHA Encryption Algorithm

SHA (Secure Hash algorithm, secure hashing algorithm), digital signature and other cryptography applications are important tools, widely used in E-commerce and other fields of information security. and MD5 encryption algorithm, is also an irreversible encryption algorithm, but can also be solved by exhaustive method, but the difficulty and cost of the interpretation of SHA is higher than MD5, compared to the MD5 more secure, now has become recognized as one of the safest hashing algorithm, and is widely used. Mainly includes sha-1,sha-224,sha-256,sha-384, and SHA-512 these several one-way hashing algorithm. 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 official commentary reads as follows:

• It is difficult to compute the original input message by the message digest.
• It is theoretically difficult to find two different sets of messages that correspond to the same message digest. Any change to the input message has a high probability that it will produce a different message digest.

The principle of SHA encryption

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.

Mac (information authentication code) is a hash result, where some of the input information is a password, only those who know the password can again calculate and verify the legality of the MAC code.

SHA Encryption Advantages

Since Sha is also a MD4 evolution, its advantages are roughly the same as MD5
• Compressibility: Any length of data, the calculation of the length of Sha value is fixed.
• Easy to calculate: It is easy to calculate the SHA value from the original data.
• Resistance to modification: Any changes to the original data, even if only modify 1 bytes, the resulting SHA value is very different.
• Strong anti-collision: Given the original data and its SHA value, it is very difficult to find a data with the same SHA value (that is, to falsify data).

sha Application Scenario

• Conformance Validation
• Digital signatures
• Secure access authentication

Simple implementation of SHA encryption

Here the code shows the example of SHA-256.

  public static String sha (String string) {
    if (Textutils.isempty (string)) {return
      "";
    }
    MessageDigest MD5 = NULL;
    try {
      MD5 = messagedigest.getinstance ("sha-256");
      byte[] bytes = Md5.digest ((string). GetBytes ());
      String result = "";
      for (byte b:bytes) {
        String temp = integer.tohexstring (b & 0xff);
        if (temp.length () = = 1) {
          temp = "0" + temp;
        }
        result = temp;
      return result;
    } catch (NoSuchAlgorithmException e) {
      e.printstacktrace ();
    }
    Return "";
  }

discussion on safety of Sha

Sha-1 is widely used in many security protocols, including TLS and SSL, PGP, SSH, S/MIME, and IPSec, and has been considered a successor to the MD5 (a hash function that was widely used earlier). In the 2005, the cipher scientists proved that SHA-1 's crack speed is 2000 times times higher than expected, although the crack is still extremely difficult and expensive, but as the computer becomes faster and cheaper, the security of the SHA-1 algorithm has been reduced, which has been seriously questioned by the cipher scientists, Hope to replace it with a higher security SHA-2.

sha-224, SHA-256, SHA-384, and SHA-512 are called SHA-2.

• The new hash function does not accept a detailed test by the public password community like SHA-1, so their password security is not widely trusted.

• Although there has not yet been an effective attack on SHA-2, its algorithm is basically still similar to SHA-1, so some people are starting to develop alternative hashing algorithms.

Multinational Deeds:
Google's official blog announces that it will gradually reduce the SHA-1 certificate's security instructions in Chrome. But interestingly, Google.com is currently using a SHA-1 signed certificate, but the certificate will expire within 3 months, and Google will use the SHA-2 signed certificate from 2015 onwards. The SHA-1 algorithm has not yet found serious weaknesses, but the cost of forging certificates is getting lower.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.