この記事では、JavaにSHA-1アルゴリズムを実装する方法について説明します。参照のためにそれを共有してください。特定の実装方法は次のとおりです。
次のようにコードをコピーします:public class sha1util {
private static final boolean hexcase = false;
プライベート静的最終文字列b64pad = "=";
Private Static Final Int Chrsz = 8;
//文字列のSHA-1値を取得する方法
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(core_hmac_sha1(key、data));
}
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+/";
文字列str = "";
binarray = strechbinarray(binarray、binarray.length * 4);
for(int i = 0; i <binary.length * 4; i += 3){
int triplet =(((binarray [i >> 2] >> 8 *(3 -i%4)&0xff)<< 16)
|。
|。
for(int j = 0; j <4; j ++){
if(i * 8 + j * 6> binary.length * 32){
str += b64pad;
} それ以外 {
str += tab.charat((triplet >> 6 *(3 -j))&0x3f);
}
}
}
cleanb64str(str)を返します。
}
private static string binb2hex(int [] binarray){
string hex_tab = hexcase?
文字列str = "";
for(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 + =(新しい文字(a).toString() + new Character(b).toString());
}
strを返します。
}
private static string binb2str(int [] bin){
文字列str = "";
int mask =(1 << chrsz)-1;
for(int i = 0; i <bin.length * 32; i += chrsz){
str +=(char)((bin [i >> 5] >>>(24 -i%32))&mask);
}
strを返します。
}
private static int bit_rol(int num、int cnt){
return(num << cnt)|。
}
private static string cleanb64str(string str){
str =(str == null)?
int len = str.length();
if(len <= 1){
strを返します。
}
Char Trailchar = str.Charat(len -1);
文字列TRAILSTR = "";
for(int i = len-1; i> = 0 && str.charat(i)== trabrach; i-){
TRAILSTR += STR.CHARAT(i);
}
return str.substring(0、str.indexof(Trailstr));
}
private static int [] complete216(int [] oldbin){
if(oldbin.length> = 16){
オールドビンを返します。
}
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];
} それ以外 {
retval [i] = newbin [i -oldbin.length];
}
}
返品返品。
}
private static int [] core_hmac_sha1(string key、string data){
key =(key == null) "":key;
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];
for(int i = 0; i <16; ipad [i] = 0、opad [i] = 0、i ++)
;
for(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、size);
x [len >> 5] | = 0x80 <<(24 -len%32);
size =((len + 64 >> 9)<< 4) + 15;
x = stretchbinarray(x、size);
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;
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 <80; j ++){
if(j <16){
w [j] = x [i + j];
} それ以外 {
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;
返品返品。
}
private static void dotest(){
文字列key = "key";
文字列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(core_hmac_sha1(key、data));
}
private static int rol(int num、int cnt){
return(num << 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)|。
}
private static int sha1_ft(int t、int b、int c、int d){
if(t <20)
return(B&C)|。
if(t <40)
b ^ c ^ dを返します。
if(t <60)
return(B&C)|。
b ^ c ^ dを返します。
}
private static int sha1_kt(int t){
return(t <20)?(t <40)?
}
private static boolean sha1_vm_test(){
hexcaseを返しますか?( "ABC")。
"A9993E364706816ABA3E25717850C26C9CD0D89D");
}
public static string str_hmac_sha1(string key、string data){
return binb2str(core_hmac_sha1(key、data));
}
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)?
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];
}
ビンを返します。
}
private static int [] Stretchbinarray(int [] oldbin、int size){
int currlen = oldbin.length;
if(currlen> = size + 1){
オールドビンを返します。
}
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];
}
Newbinを返します。
}
public static void main(string args []){
system.out.println( "adminのsha1値は次のとおりです。
}
}
この記事がみんなのJavaプログラミングに役立つことを願っています。