This paper illustrates the method of Java implementation SHA-1 algorithm. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
public class Sha1util {
Private static Final Boolean hexcase = false;
private static final String B64pad = "=";
private static final int chrsz = 8;
method to get SHA-1 value of string
public static string Hex_sha1 (string s) {
s = (s = = null)? "": s;
Return Binb2hex (CORE_SHA1 (STR2BINB (s), s.length () * chrsz));
}
public static string B64_hmac_sha1 (string key, string data) {
Return binb2b64 (key, data) (CORE_HMAC_SHA1);
}
public static string B64_sha1 (string s) {
s = (s = = null)? "": s;
Return binb2b64 (CORE_SHA1 (STR2BINB (s), s.length () * chrsz));
}
private static String binb2b64 (int[] binarray) {
String tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
String str = "";
BinArray = Strechbinarray (BinArray, Binarray.length * 4);
for (int i = 0; i < binarray.length * 4; i + + 3) {
int triplet = ((binarray[i >> 2] >> 8 * (3-i% 4)) & 0xFF) << 16)
| (((binarray[i + 1 >> 2] >> 8 * (3-(i + 1)% 4)) & 0xFF) << 8)
| ((Binarray[i + 2 >> 2] >> 8 * (3-(i + 2)% 4)) & 0xFF);
for (int j = 0; J < 4; J + +) {
if (I * 8 + J * 6 > Binarray.length * 32) {
str = B64pad;
} else {
str = Tab.charat ((triplet >> 6 * (3-J)) & 0x3f);
}
}
}
return Cleanb64str (str);
}
private static String Binb2hex (int[] binarray) {
String Hex_tab = hexcase? "0123456789abcdef": "0123456789abcdef";
String str = "";
for (int i = 0; i < binarray.length * 4; i++) {
char a = (char) hex_tab.charat ((Binarray[i >> 2] >> ((3-i% 4) * 8 + 4)) & 0xf);
Char B = (char) hex_tab.charat ((Binarray[i >> 2] >> ((3-i% 4) * 8)) & 0xf);
str = (new Character (a). ToString () + new Character (b). ToString ());
}
return str;
}
private static String Binb2str (int[] bin) {
String str = "";
int mask = (1 << chrsz)-1;
for (int i = 0; i < bin.length * i + + = Chrsz) {
STR + + (char) ((Bin[i >> 5] >>> (24-i%)) & mask);
}
return str;
}
private static int Bit_rol (int num, int cnt) {
return (num << cnt) | (Num >>> (32-cnt));
}
private static string Cleanb64str (String str) {
str = (str = null)? "": STR;
int len = Str.length ();
if (len <= 1) {
return str;
}
Char Trailchar = Str.charat (len-1);
String trailstr = "";
for (int i = len-1 I >= 0 && str.charat (i) = = Trailchar; i--) {
Trailstr + + Str.charat (i);
}
Return str.substring (0, Str.indexof (trailstr));
}
private static int[] complete216 (int[] oldbin) {
if (oldbin.length >= 16) {
return oldbin;
}
int[] Newbin = new Int[16-oldbin.length];
for (int i = 0; i < newbin.length newbin[i] = 0, i++)
;
Return concat (Oldbin, Newbin);
}
private static int[] Concat (int[] oldbin, int[] newbin) {
int[] retval = new Int[oldbin.length + newbin.length];
for (int i = 0; I < (oldbin.length + newbin.length); i++) {
if (I < oldbin.length) {
Retval[i] = Oldbin[i];
} else {
Retval[i] = Newbin[i-oldbin.length];
}
}
return retval;
}
private static int[] CORE_HMAC_SHA1 (string key, string data) {
Key = (key = null)? "": Key;
data = (data = null)? "": Data;
int[] Bkey = complete216 (STR2BINB (key));
if (Bkey.length > 16) {
Bkey = CORE_SHA1 (Bkey, Key.length () * Chrsz);
}
int[] ipad = new INT[16];
int[] Opad = new INT[16];
for (int i = 0; i < ipad[i] = 0, Opad[i] = 0, i++)
;
for (int i = 0; i < i++) {
Ipad[i] = bkey[i] ^ 0x36363636;
Opad[i] = bkey[i] ^ 0x5c5c5c5c;
}
Int[] hash = core_sha1 (ipad, STR2BINB (data)), Concat + data.length () * Chrsz);
Return Core_sha1 (concat (Opad, hash), 512 + 160);
}
private static int[] CORE_SHA1 (int[] x, int len) {
int size = (len >> 5);
x = Strechbinarray (x, size);
X[len >> 5] |= 0x80 << (24-len% 32);
Size = ((len + >> 9) << 4) + 15;
x = Strechbinarray (x, size);
x[(len + >> 9) << 4) = Len;
int[] W = new int[80];
int a = 1732584193;
int b =-271733879;
int c =-1732584194;
int d = 271733878;
int e =-1009589776;
for (int i = 0; i < x.length i + + 16) {
int Olda = A;
int oldb = b;
int OLDC = c;
int oldd = D;
int olde = e;
for (int j = 0; J <; J + +) {
if (J < 16) {
W[J] = X[i + j];
} else {
W[J] = Rol (w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
}
int t = safe_add (Safe_add (Rol (A, 5), sha1_ft (J, B, C, D)), Safe_add (Safe_add (E, w[j)), Sha1_kt (j)));
e = D;
D = C;
c = Rol (b, 30);
b = A;
A = t;
}
A = Safe_add (A, Olda);
b = Safe_add (b, oldb);
c = Safe_add (c, OLDC);
D = Safe_add (d, OLDD);
E = Safe_add (e, Olde);
}
int[] retval = new INT[5];
Retval[0] = A;
RETVAL[1] = b;
RETVAL[2] = c;
RETVAL[3] = D;
RETVAL[4] = e;
return retval;
}
private static void Dotest () {
String key = "key";
String data = "Data";
System.out.println ("HEX_SHA1 (" + data + ") =" + HEX_SHA1 (data));
System.out.println ("B64_SHA1 (" + data + ") =" + B64_SHA1 (data));
System.out.println ("STR_SHA1 (" + data + ") =" + STR_SHA1 (data));
System.out.println ("HEX_HMAC_SHA1 + key +", "+ Data +") = "+ HEX_HMAC_SHA1 (key, data));
System.out.println ("B64_HMAC_SHA1 + key +", "+ Data +") = "+ B64_HMAC_SHA1 (key, data));
System.out.println ("STR_HMAC_SHA1 + key +", "+ Data +") = "+ STR_HMAC_SHA1 (key, data));
}
public static string Hex_hmac_sha1 (string key, string data) {
Return Binb2hex (key, data) (CORE_HMAC_SHA1);
}
private static int rol (int num, int cnt) {
return (num << cnt) | (Num >>> (32-cnt));
}
private static int Safe_add (int x, int y) {
int LSW = (int) (x & 0xFFFF) + (int) (Y & 0xFFFF);
int MSW = (x >>) + (y >>) + (LSW >> 16);
Return (MSW << 16) | (LSW & 0xFFFF);
}
private static int sha1_ft (int t, int b, int c, int d) {
if (T < 20)
Return (b & c) | ((~b) & D);
if (T < 40)
Return b ^ C ^ D;
if (T < 60)
Return (b & c) | (b & D) | (C & D);
Return b ^ C ^ D;
}
private static int sha1_kt (int t) {
Return (T < 20)? 1518500249: (T < 40)? 1859775393: (T < 60)? -1894007588:-899497514;
}
private static Boolean sha1_vm_test () {
Return hexcase? HEX_SHA1 ("abc"). Equals ("a9993e364706816aba3e25717850c26c9cd0d89d"): HEX_SHA1 ("abc"). Equals (
"a9993e364706816aba3e25717850c26c9cd0d89d");
}
public static string Str_hmac_sha1 (string key, string data) {
Return Binb2str (key, data) (CORE_HMAC_SHA1);
}
public static string Str_sha1 (string s) {
s = (s = = null)? "": s;
Return Binb2str (CORE_SHA1 (STR2BINB (s), s.length () * chrsz));
}
private static int[] Str2binb (String str) {
str = (str = null)? "": STR;
int[] tmp = new Int[str.length () * Chrsz];
int mask = (1 << chrsz)-1;
for (int i = 0; i < str.length () * chrsz i = = Chrsz) {
Tmp[i >> 5] |= ((int) (Str.charat (I/chrsz)) & mask) << (24-i% 32);
}
int len = 0;
for (int i = 0; i < tmp.length && Tmp[i]!= 0; i++, len++)
;
int[] bin = new Int[len];
for (int i = 0; i < len; i++) {
Bin[i] = Tmp[i];
}
return bin;
}
private static int[] Strechbinarray (int[] oldbin, int size) {
int currlen = Oldbin.length;
if (currlen >= size + 1) {
return oldbin;
}
int[] Newbin = new Int[size + 1];
for (int i = 0; i < size; Newbin[i] = 0, i++)
;
for (int i = 0; i < Currlen; i++) {
Newbin[i] = Oldbin[i];
}
return newbin;
}
public static void Main (String args[]) {
System.out.println ("admin's SHA1 value is:" + hex_sha1 ("admin") + ", length=" + hex_sha1 ("admin"). Length ());
}
}
I hope this article will help you with your Java programming.