Principle and common implementation of SHA algorithm

Before reading this article, it is best to take a look at the previous introduction to the MD5 algorithm.
MD5 algorithm principle and common implementation

definition

SHA algorithm (Secure Hash Algorithm), also known as secure hash algorithm.
The SHA algorithm is based on the MD4 algorithm and evolved.
But the SHA algorithm was born and designed by the National Security Agency.
The SHA algorithm is a series of families, including SHA-1 and SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512). The four in parentheses are commonly referred to as SHA-2.
JDK implements SHA-1, SHA-256, SHA-384, and SHA-512.
The final password length of SHA-1 is 160 bits.
The final password length of SHA-256 is 256 bits.
The final password length of SHA-384 is 384 bits.
The final password length of SHA-512 is 512 bits.
The SHA algorithm is actually a message digest algorithm, which is similar to the MD algorithm.

This article mainly introduces the SHA-256 algorithm.
Message files of any length are encrypted by the SHA-256 algorithm, and the resulting ciphertext is 256 bits (32 bytes), usually represented by a hexadecimal string with a length of 64.

In effect, the SHA algorithm is similar to the MD5 algorithm, and the main features are irreversible.

MD5 and SHA-1 collision problem

The collision problem refers to that for an algorithm, the ciphertext generated from the plaintext is not unique, and even a new ciphertext can be artificially constructed to obtain the specified ciphertext. For the algorithm that can appear in this situation, we say that this algorithm has a collision problem. The simple understanding is that two different plaintexts are encrypted by the algorithm, but the same ciphertext is obtained.

So far, the MD5 algorithm and the SHA-1 algorithm have been confirmed to have collision problems.
Therefore, in scenarios with particularly high security, the MD5 algorithm and the SHA-1 algorithm will not be used, at least the SHA-256 algorithm will be used.

Common application scenarios

1. Application scenarios similar to MD5

MD5 application scenarios, the SHA algorithm can basically be used.

2. Bitcoin

In Bitcoin, the mining algorithm is actually the SHA-256 algorithm. The miners constantly modify the random number and constantly perform the SHA-256 operation, and finally calculate the mining speed.

3. https signature algorithm will be used

Open the browser and take Google as an example to view the details of any integer: the
signature algorithm is: SHA-256 with RSA encryption, as shown below:
Insert picture description here

Principle of SHA-256 algorithm

The basic algorithm ideas of the SHA family are the same as those of MD5. First define the constants, then calculate the loop, and finally assemble. The difference is the calculation method in the loop.

1. Fill in the information

Similar to MD5

2. Get the initial value

There are 4 initial values ​​in MD5, and 8 in SHA-256.

h0 := 0x6a09e667
h1 := 0xbb67ae85
h2 := 0x3c6ef372
h3 := 0xa54ff53a
h4 := 0x510e527f
h5 := 0x9b05688c
h6 := 0x1f83d9ab
h7 := 0x5be0cd19

3. Real calculation

The calculation is divided into multiple cycles. Each cycle is filled with the information filled in the first step with ABCD and the original text. The calculation is performed, and finally a new ABCD is obtained. Finally, the last ABCD is spelled into a string, which is the final ciphertext.
The loop is first divided into a main loop, and each main loop is set with a sub-loop.
Number of main loops = length of original text / 512.
Number of sub-cycles = 64 times.

Let's take a look at what the single sub-loop does:
The following is the real calculation logic of the single sub-loop (this implementation is taken from netizens):
Insert picture description here

Java implementation and use

In fact, after reading the previous MD5 introduction, you will find that the Java implementation of SHA is almost the same as the Java implementation of MD5.
The only difference is MessageDigest.getInstance ("SHA"); In this method: The
input parameter of SHA-1 algorithm
is SHA-256 The
input parameter of SHA algorithm is SHA-256 The input parameter of SHA-384 algorithm is SHA-384
SHA -512 algorithm input parameter is SHA-512
MD5 input parameter is MD5

public class SHA1Util {
    public static void main(String[] args) throws IOException {
        System.out.println(encodeString("123"));
    }

    public static String encodeString(String plainText) throws UnsupportedEncodingException {
        return encodeBytes(plainText.getBytes("UTF-8"));
    }

    public static String encodeBytes(byte[] bytes) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(bytes);
            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}

Published 203 original articles · praised 186 · 210,000 views

Guess you like

Origin blog.csdn.net/java_zhangshuai/article/details/105590847