Dieser Artikel beschreibt die Methode zur Implementierung des SHA-1-Algorithmus in Java. Teilen Sie es für Ihre Referenz. Die spezifische Implementierungsmethode lautet wie folgt:
Kopieren Sie den Code wie folgt: öffentliche Klasse SHA1UTIL {
privates statisches endgültiges Boolean Hexcase = Falsch;
private statische endgültige Zeichenfolge b64pad = "=";
Private statische endgültige int chrsz = 8;
// Methode, um den SHA-1-Wert der Zeichenfolge zu erhalten
public static String hex_sha1 (String s) {
S = (s == null)?
return binb2hex (core_sha1 (str2binb (s), s.Length () * chrsz));
}
public static String B64_HMAC_SHA1 (String -Schlüssel, String -Daten) {
return binb2b64 (core_hmac_sha1 (Schlüssel, Daten));
}
public statische Zeichenfolge B64_SHA1 (String S) {
S = (s == null)?
return binb2b64 (core_sha1 (str2binb (s), s.Length () * chrsz));
}
private statische String -Binb2b64 (int [] binarray) {
String tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklMnopqrstuvwxyz0123456789+/";
String str = "";
binarray = stechbinarray (binarray, binarray.length * 4);
für (int i = 0; i <binary.length * 4; i += 3) {
int triplet = (((binarray [i >> 2] >> 8 * (3 - i % 4)) & 0xff) << 16)
|.
|.
für (int j = 0; j <4; j ++) {
if (i * 8 + j * 6> binary.length * 32) {
str += b64pad;
} anders {
str += tab.charat ((Triplet >> 6 * (3 - j)) & 0x3f);
}
}
}
return cleanB64str (str);
}
private statische String binb2hex (int [] Binarray) {
String hex_tab = hexcase?
String str = "";
für (int i = 0; i <binary.length * 4; i ++) {
char a = (char) hex_tab.charat ((binArray [>> 2] >> ((3 - i % 4) * 8 + 4)) & 0xf);
char b = (char) hex_tab.charat ((binarray [>> 2] >> ((3 - i % 4) * 8)) & 0xf);
str + = (neuer Zeichen (a) .ToString () + neues Zeichen (b) .ToString ());
}
return str;
}
private statische String binb2str (int [] bin) {
String str = "";
int mask = (1 << chrsz) - 1;
für (int i = 0; i <bin.Length * 32; i += chrsz) {
STR += (char) ((bin [i >> 5] >>> (24 - I % 32)) & Mask);
}
return str;
}
private static int bit_rol (int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
private statische String CleanB64str (String Str) {
STR = (STR == NULL)?
int len = str.length ();
if (len <= 1) {
return str;
}
char TrailChar = str.charat (len - 1);
String TrailStr = "";
für (int i = len-1; i> = 0 && str.charat (i) == Trailchar; i--) {
TrailStr += str.charat (i);
}
return str.substring (0, str.sindexof (TrailStr));
}
private static int [] complete216 (int [] Oldbin) {
if (Oldbin.length> = 16) {
kehre Oldbin zurück;
}
int [] newbin = new int [16 - Oldbin.length];
für (int i = 0; i <newbin.length; newbin [i] = 0, i ++)
;
Returnconat (Oldbin, Newbin);
}
private static int [] concat (int [] Oldbin, int [] Newbin) {
int [] retval = new int [oldbin.length + newbin.length];
für (int i = 0; i <(oldBin.length+newbin.length); i ++) {
if (i <oldbin.length) {
retval [i] = Oldbin [i];
} anders {
retval [i] = newbin [i - Oldbin.length];
}
}
Rückgaberückgabe;
}
private static int [] core_hmac_sha1 (String -Schlüssel, String -Daten) {
key = (key == null)?
Data = (Data == NULL)?
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];
für (int i = 0; i <16; iPad [i] = 0, opad [i] = 0, i ++)
;
für (int i = 0; i <16; i ++) {
iPad [i] = bkey [i] ^ 0x36363636;
opad [i] = bkey [i] ^ 0x5c5c5c5c;
}
int [] Hash = core_sha1 (concat (iPad, str2binb (data)), 512 + 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 = Stretchbinarray (x, Größe);
x [len >> 5] | = 0x80 << (24 - Len % 32);
size = ((len + 64 >> 9) << 4) + 15;
x = Stretchbinarray (x, Größe);
x [((len + 64 >> 9) << 4) + 15] = len;
int [] w = new int [80];
int a = 1732584193;
int b = -271733879;
int c = -1732584194;
int d = 271733878;
int e = -1009589776;
für (int i = 0; i <x.Length; i += 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int Olde = e;
für (int j = 0; j <80; j ++) {
if (j <16) {
w [j] = x [i + j];
} anders {
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;
Rückgaberückgabe;
}
private statische 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 (Schlüssel, Daten));
System.out.println ("b64_hmac_sha1 (" + key + "," + data + ") =" + B64_HMAC_SHA1 (Schlüssel, Daten));
System.out.println ("str_hmac_sha1 (" + key + "," + data + ") =" + str_hmac_sha1 (Schlüssel, Daten));
}
public static String hex_hmac_sha1 (String -Schlüssel, String -Daten) {
return Binb2hex (core_hmac_sha1 (Schlüssel, Daten));
}
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 >> 16) + (y >> 16) + (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)
Rückkehr b ^ c ^ d;
if (t <60)
Return (B & C) | (B & D) |
Rückkehr b ^ c ^ d;
}
private static int sha1_kt (int t) {
Return (T <20)?
}
private statische boolean sha1_vm_test () {
Hexcase zurückgeben?
"A9993E364706816ABA3E25717850C26C9CD0D89D");
}
public static String str_hmac_sha1 (String -Schlüssel, String -Daten) {
return binb2str (core_hmac_sha1 (Schlüssel, Daten));
}
public static String str_sha1 (String s) {
S = (s == null)?
return binb2str (core_sha1 (str2binb (s), s.Length () * chrsz));
}
private static int [] str2binb (String str) {
STR = (STR == NULL)?
int [] tmp = new int [str.length () * chrsz];
int mask = (1 << chrsz) - 1;
für (int i = 0; i <str.length () * chrsz; i += chrsz) {
tmp [i >> 5] | = ((int) (str.charat (i / chrsz)) & mask) << (24 - i % 32);
}
int len = 0;
für (int i = 0; i <tmp.length && tmp [i]! = 0; i ++, len ++)
;
int [] bin = new int [len];
für (int i = 0; i <len; i ++) {
bin [i] = tmp [i];
}
Rückzug;
}
Private statische int [] Stretchbinarray (int [] Oldbin, int Größe) {
int currlen = oldBin.length;
if (Currlen> = Größe + 1) {
kehre Oldbin zurück;
}
int [] newbin = new int [Größe + 1];
für (int i = 0; i <size; newbin [i] = 0, i ++)
;
für (int i = 0; i <currlen; i ++) {
Newbin [i] = Oldbin [i];
}
Return Newbin;
}
public static void main (String args []) {
System.out.println ("admin sha1 -Wert ist:" + hex_sha1 ("admin") + ", Länge =" + hex_sha1 ("admin"). Länge ());
}
}
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.