Cet article décrit la méthode de mise en œuvre de l'algorithme SHA-1 en Java. Partagez-le pour votre référence. La méthode de mise en œuvre spécifique est la suivante:
Copiez le code comme suit: classe publique Sha1Util {
Hexcase booléen final statique privé = false;
chaîne finale statique privée b64pad = "=";
Final statique privé int chsz = 8;
// Méthode pour obtenir la valeur Sha-1 de la chaîne
String statique public hex_sha1 (String S) {
S = (s == null)? ": S;
return binb2hex (core_sha1 (str2binb (s), s.length () * chrsz));
}
chaîne statique publique b64_hmac_sha1 (clé de chaîne, données de chaîne) {
return binb2b64 (core_hmac_sha1 (clé, données));
}
String statique publique B64_Sha1 (String S) {
S = (s == null)? ": S;
return binb2b64 (core_sha1 (str2binb (s), s.length () * chrsz));
}
chaîne statique privée binb2b64 (int [] binarray) {
String tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 + /";
String str = "";
binarray = strechbinarray (binarray, binarray.length * 4);
pour (int i = 0; i <binary.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);
pour (int j = 0; j <4; j ++) {
if (i * 8 + j * 6> binary.length * 32) {
str + = b64pad;
} autre {
str + = tab.charat ((triplet >> 6 * (3 - j)) & 0x3f);
}
}
}
Retour CleanB64STR (STR);
}
chaîne statique privée binb2hex (int [] binArray) {
String hex_tab = hexcase? "0123456789abcdef": "0123456789abcdef";
String str = "";
pour (int i = 0; i <binary.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 + = (nouveau caractère (a) .toString () + nouveau caractère (b) .toString ());
}
retour STR;
}
chaîne statique privée binb2str (int [] bin) {
String str = "";
int mask = (1 << chrsz) - 1;
for (int i = 0; i <bin.length * 32; i + = chrsz) {
str + = (char) ((bin [i >> 5] >>> (24 - i% 32)) & masque);
}
retour STR;
}
privé static int bit_rol (int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
String statique privé CleanB64Str (String Str) {
str = (str == null)? ": str;
int len = str.length ();
if (len <= 1) {
retour STR;
}
char trailchar = str.charat (len - 1);
String trailstr = "";
pour (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 [] complet216 (int [] oldbin) {
if (oldbin.length> = 16) {
Retour Oldbin;
}
int [] newbin = new int [16 - oldbin.length];
for (int i = 0; i <newbin.length; newbin [i] = 0, i ++)
;
RETOUR 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];
} autre {
retval [i] = newbin [i - oldbin.length];
}
}
retour de retour retour;
}
private static int [] core_hmac_sha1 (clé de chaîne, données de chaîne) {
key = (key == null)? ": key;
data = (data == null)? ": données;
int [] bkey = complet216 (str2binb (key));
if (bkey.length> 16) {
bkey = core_sha1 (bkey, key.length () * chrsz);
}
int [] iPad = new int [16];
int [] opad = new int [16];
pour (int i = 0; i <16; iPad [i] = 0, opad [i] = 0, i ++)
;
pour (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);
}
privé static int [] core_sha1 (int [] x, int len) {
int size = (len >> 5);
x = stretchinarray (x, taille);
x [len >> 5] | = 0x80 << (24 - len% 32);
size = ((len + 64 >> 9) << 4) + 15;
x = stretchinarray (x, taille);
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;
pour (int i = 0; i <x.length; i + = 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int olde = e;
pour (int j = 0; j <80; j ++) {
if (j <16) {
w [j] = x [i + j];
} autre {
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;
retour de retour retour;
}
private static void dectest () {
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));
}
String statique publique hex_hmac_sha1 (clé de chaîne, données de chaîne) {
return binb2hex (core_hmac_sha1 (clé, données));
}
Int rol statique privé (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);
}
statique privée int sha1_ft (int t, int b, int c, int d) {
si (t <20)
retour (b & c) | ((~ b) & d);
si (t <40)
retour b ^ c ^ d;
si (t <60)
Retour (B&C) | (B&D) | (C & D);
retour b ^ c ^ d;
}
privé statique int sha1_kt (int t) {
Retour (T <20)?
}
booléen statique privé sha1_vm_test () {
retour hexcase?
"A9993E364706816ABA3E25717850C26C9CD0D89D");
}
String statique public str_hmac_sha1 (clé de chaîne, données de chaîne) {
return binb2str (core_hmac_sha1 (key, data));
}
chaîne statique publique str_sha1 (chaîne s) {
S = (s == null)? ": S;
return binb2str (core_sha1 (str2binb (s), s.length () * chrsz));
}
Int statique privé [] str2binb (String str) {
str = (str == null)? ": str;
int [] tmp = new int [str.length () * chrsz];
int mask = (1 << chrsz) - 1;
pour (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];
pour (int i = 0; i <len; i ++) {
bin [i] = tmp [i];
}
Bin de retour;
}
private static int [] stretperbinarray (int [] oldbin, int taille) {
int Currlen = oldbin.length;
if (currlen> = taille + 1) {
retourner oldbin;
}
int [] newbin = new int [size + 1];
pour (int i = 0; i <size; newbin [i] = 0, i ++)
;
pour (int i = 0; i <currlen; i ++) {
newbin [i] = oldbin [i];
}
retourner newbin;
}
public static void main (String args []) {
System.out.println ("La valeur SHA1 de l'administrateur est:" + hex_sha1 ("admin") + ", longueur =" + hex_sha1 ("admin"). Length ());
}
}
J'espère que cet article sera utile à la programmation Java de tous.