Tools that can perform SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 five algorithm signatures, and simple instructions

import java.security.MessageDigest;

public class SignatureSHA {
public static String signSHA(String sha,Object ...arguments){
try{
StringBuffer stringSignTemp = new StringBuffer();
for (int i = 0; i < arguments.length; i++) {
stringSignTemp.append(arguments[i]);
}
System.out.println("stringSignTemp:"+stringSignTemp);
MessageDigest sha1 = MessageDigest.getInstance(sha);//可以进行,SHA-1,SHA-224,SHA-256,SHA-384,SHA-512五种算法签名
sha1.update(stringSignTemp.toString().getBytes("utf-8"));
byte[] sha1Bytes = sha1.digest();
String sign = bin2hex(sha1Bytes);
return sign.toString();
}catch(Exception e){
return null;
}
}
public static String bin2hex(byte[] bin)
{
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
StringBuilder sb = new StringBuilder();
for (int i = 0 ; i < bin.length; i++)
{
char upper = hex[(bin[i] & 0xf0) >>> 4];
char lower = hex[(bin[i] & 0x0f)];
sb = sb.append(upper).append(lower);
}
return sb.toString();
}
public static void main(String[] args) {
String sign=signSHA("SHA-224","123",111,888);//假设123是加密密钥
System.out.println(sign);
}
}

SHA is a commonly used data encryption algorithm.

Features: 1. The length of the secret after encryption is fixed; 2. Like md5 encryption, it is irreversible and cannot be decrypted;

Verification method: data + key, the key is a string of any length, the data that needs to be signed, the key and the data are spelled together, and then encrypted.

The encryption principles of the five algorithm signatures of SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are similar.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326268186&siteId=291194637