Java version of SHA-1

xiaoxiao2021-03-06  113

/ **

* this Java Class Consists The Server Side for the Wondeful JavaScript library 'sha1.js'. I wrote it because i Basical Needed

* Some Cheap Client / Server Login Authentication by The Usual Key / Data System. Besides, I Got The Creeps Watching The Password

* Posted Unencrypted Via Http Requests. this class makess sure what if you client is using the 'sha1.js' to encrypt the password

* with a key service by the server, you can always repeat the encrypting on the server side (useing the same key) and compare the

* Encrypted strings. Sincenyone who is trapping the http requests can actually send you the Same Encrypted String, i suggest

* You Use the client's ip address as the base for the key generation. Since ip address spoofing is not a problem, this Authentication

* Method is not a Very SecureD solution. If you need a full. However, this ONE, SURE beats Nothing.

* Feel free to do with it whosever you want

*

this class is an abstract class, to make you do not create any new instances of it. it does not throw anything ip

* The code is much more 'c' like tour object Oriented. There received interfaces and no inheritance in use. in fabs

* is Written As Close As Possible To The Original JavaScript Code. I Did NOT TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST Test Test Test Test Test Test Test Code.

* The Make Sure to Apply The Same Change in The 'Sha1.js' Library Or You Won't get The Same Encrypted strings. 0000-00-00 0000-00-00

* You Can Call Each ONE OF THE 6 WORK Methods by Using Something Like: Sha1.Hex_hmac_sha1 ("Key", "DATA");

* They is the only public and static. You have no reference and static. You have no reason. *

the 'sha1.js' is a javascript usteation of the second Hash Algorithm, SHA-1, AS Defined IN FIPS PUB 180-1.

* JavaScript Version 2.1 Copyright Paul Johnston 2000 - 2002. Other Contributors to JavaScript Version: Greg Holt,

* Andrew Kepert, YDNAR, LOSTINET Distributed Under The BSD License

*

see http://pajhome.org.uk/crypt/md5 for details.

*

author: t.n.silverman (c.t.xm - sia riga, lv) mailto: tnsilver@ctxm.com

*
Creation Date: (3/27/2004 5:57:00 PM)

*

don't forget to visit my company, ctxm site at http://www.ctxm.com Where you will find reference to all of the games this code is used in.

* /

Public Abstract Class Sha1 {

Private static final boolean hexcase = false; / * hex output format. false - lowercase; true - uppercase * /

Private static final string b64pad = "="; / * base-64 pad character. "=" = "for strict rfc company"

Private static final int chrs = 8; / * bits per input character. 8 - ASCII; 16 - Unicode * /

/ **

* This is one of the functions you'll uasually want to call

* IT Take A String Arguments and return Either Hex Or Base-64 Encode Strings

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Key java.lang.string

* @Param Data Java.lang.string * /

Public Static String B64_HMAC_SHA1 (String Key, String Data) {

Return binb2b64 (Core_HMAC_SHA1 (KEY, DATA));

}

/ **

* This is one of the functions you'll uasually want to call

* IT Take A String Argument and returns Either Hex Or Base-64 Encode Strings

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param s java.lang.string

* /

Public Static String B64_SHA1 (String S) {

s = (s == null)? "": s;

Return BINB2B64 (Core_SHA1 (STR2binb (s), S.Length () * chrsz);

}

/ **

* Convert An Array of Big-endian Words to a base-64 string

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Binarray Int []

* /

Private static string binb2b64 (int [] binArray) {

String Tab = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /";

String str = "";

BINARRAY = strechbinarray (binarray, binarray.length * 4);

For (int i = 0; i

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);

}

/ **

* Convert An Array of Big-endian Words to a hex string.

* Creation Date: (3/27/2004 6:05:10 pm) * @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Binarray Int []

* /

Private static string binb2hex (int [] binaRray) {

String hex_tab = hexcase? "0123456789abcdef": "0123456789Abcdef";

String str = "";

For (int i = 0; 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;

}

/ **

* Convert An Array of Big-Endian Words to a string

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Bin Int []

* /

Private statçing binb2str (int [] bin) {

String str = "";

INT MASK = (1 << chrsz) - 1;

For (int i = 0; i

STR = (CHAR) (BIN [I >> 5] >>> (24 - I% 32)) & Mask

Return Str;

}

/ **

* Bitwise Rotate a 32-bit number to the left.

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param Num Int

* @Param CNT INT

* /

Private static int bit_rol (int Num, int CNT) {

Return (NUM << CNT) | (NUM >>> (32 - CNT));

}

/ **

* Cleans a base64 string from all the trailing 'a' or other

* Characters Put there by binb2b64 That Made the bin array

* 4 Times Larger Than it Originally Was.

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman * @version 1.0.0

* @Return java.lang.string

* @Param Str java.lang.string

* /

Private static string cleanb64str (string str) {

Str = (str == null)? ": STR;

INT LEN = str.length ();

IF (len <= 1)

Return Str;

Char trailcha = 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));

}

/ **

* Makes An Int Array of a Length Less Than 16 An Array of Length 16 with All Previous

* Cells at their previous indexes.

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int []

* @Param Str java.lang.string

* /

Private static int [] completion216 (int [] oldbin) {

IF (oldbin.length> = 16)

Return Oldbin;

int [] newbin = new int [16 - oldbin.length];

For (int i = 0; I

Return Concat (Oldbin, Newbin);

}

/ **

* Joins Two int Arrays and return One That Contains All The Previous Values.

* This Corresponds to the Concat Method of the JavaScript Array Object.

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int []

* @Param Str java.lang.string

* /

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

RetVal [I] = Oldbin [i];

Else

RetVal [I] = newbin [i - oldbin.length];

}

Return RetVal;

}

/ **

* Calculate the hmac-sha1 of a key and some data * code date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param x java.lang.string []

* @Param Len Int

* /

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 <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);

}

/ **

* Calculate the sha-1 of an array of big-endian words, and a bit length

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param x java.lang.string []

* @Param Len Int

* /

Private static int [] core_sha1 (int []) {

/ * append padding * /

INT size = (Len >> 5);

X = strechbinarray (x, size);

X [Len >> 5] | = 0x80 << (24 - len% 32);

Size = ((Len 64 >> 9) << 4) 15;

X = strechbinarray (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

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];

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;

}

/ **

* Just A Test Function To Output The Results of The 6 Working Funcions to the Standard Out.

* The Two strings Used as parameters are null. Feel Free To Test with Different Values.

* Creation Date: (3/27/20046: 05: 10pm)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* /

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));}

/ **

* This is one of the functions you'll uasually want to call

* IT Take A String Arguments and return Either Hex Or Base-64 Encode Strings

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Key java.lang.string

* @Param Data Java.lang.String

* /

Public static string hex_hmac_sha1 (String Key, String Data) {

Return binb2hex (Core_HMAC_SHA1 (Key, DATA);

}

/ **

* This is one of the functions you'll uasually want to call

* IT Take A String Argument and returns Either Hex Or Base-64 Encode Strings

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param s java.lang.string

* /

Public Static String HEX_SHA1 (String s) {

s = (s == null)? "": s;

Return binb2hex (Core_SHA1 (Str2binb (s), S.Length () * chrs);

}

/ **

* Bitwise Rotate a 32-bit number to the left. * Creation Date: (3/26/2004 1:05:01 PM)

* Creation Date: (3/27/2004 6:05:10 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param Num Int

* @Param CNT INT

* /

Private Static int ROL (int Num, int CNT) {

Return (NUM << CNT) | (NUM >>> (32 - CNT));

}

/ **

* Add INTS, Wraping AT 2 ^ 32. This Uses 16-bit Operations Internally

* To work around bugs in some js interpreters. The Original Function

* is part of the sha1.js library. it's here for compatibility.

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param Num Int * @Param CNT INT

* /

Private static int SAFE_ADD (int X, int y) {

INT LSW = (int) (x & 0xfff) (int) (Y & 0xFFFF);

INT MSW = (x >> 16) (Y >> 16) (LSW >> 16);

Return (MSW << 16) | (LSW & 0xFFF);

}

/ **

* Perform the appropriate Triplet Combination Function for the Current

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param T INT

* @Param B INT

* @Param C int

* @Param D int

* /

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;

}

/ **

* DETERMINE The Appropriate Additive Constant for the Current Iteration

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int

* @Param T INT

* /

Private static int SHA1_KT (INT T) {

Return (T <20)

? 1518500249

: (t <40)

? 1859775393

(t <60)

? -1894007588

: -899497514;

}

/ **

. --... The sha1.js library.

* If it returns 'false' something is wrong.

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param s java.lang.string

* /

Private static boolean SHA1_VM_TEST () {

RETURN HEXCASE? HEX_SHA1 ("ABC"). Equals ("A9993E364706816ABA3E25717850C26C9CD0D89D"): HEX_SHA1 ("ABC"). Equals ("A9993E364706816ABA3E25717850C26C9CD0D89D");

}

/ **

* This is one of the functions you'll uasually want to call * it take A string arguments and return Either Hex or Base-64 Encode Strings

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param Key java.lang.string

* @Param Data Java.lang.String

* /

Public static string str_hmac_sha1 (String Key, String Data) {

Return Binb2Str (Core_HMAC_SHA1 (KEY, DATA));

}

/ **

* This is one of the functions you'll uasually want to call

* IT Take A String Argument and returns Either Hex Or Base-64 Encode Strings

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return java.lang.string

* @Param s java.lang.string

* /

Public Static String Str_SHA1 (String S) {

s = (s == null)? "": s;

Return Binb2Str (Core_SHA1 (STR2binb (s), S.Length () * chrs);

}

/ **

* Convert An 8-bit or 16-bit string to an array of big-endian words

* In 8-bit function, Characters> 255 Have their hi-byte silently ignored.

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int []

* @Param Str java.lang.string

* /

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

TMP [I >> 5] | = ((int) (Str.Charat (I / Chrsz)) & mask << (24 - i% 32);

INT LEN = 0;

For (int i = 0; i

int [] bin = new int [len];

For (int i = 0; i

BIN [I] = TMP [I]; Return Bin;

}

/ **

* Increase An int Array to a desired sized 1 While Keeping The Old Values.

* Creation Date: (3/26/2004 1:05:01 PM)

* @Author t.n.silverman

* @version 1.0.0

* @Return Int []

* @Param Str java.lang.string

* /

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

For (int i = 0; i

Newbin [i] = oldbin [i];

Return newbin;

}

}


New Post(0)