webservice(soap)接口的加密,SHA-1实现

时间:2023-03-08 21:47:21

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SoapKey {
private static String byteArrayToHex(byte[] byteArray) {
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char[] resultCharArray = new char[byteArray.length * 2];
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
return new String(resultCharArray);
}

//计算消息摘要
public static String getMessageDigest(String str, String encName) {
byte[] digest = null;
try {
MessageDigest md = MessageDigest.getInstance(encName);
md.update(str.getBytes("utf-8"));
digest = md.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {

e.printStackTrace();
}
return byteArrayToHex(digest);
}
}