Java version of SHA-1

xiaoxiao2021-03-06  93

/ ** * This Java Class consists the server side for the wondeful JavaScript library 'sha1.js'. I wrote it because I basically 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 makes sure that if your client is using the 'sha1.js' to encrypt the password * with a key sent by the server, you can always repeat the encrypting on the server side (using the same key) and compare the * encrypted strings. Since anyone 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 proof solution use ssl. However, this one, sure beats nothing. * Feel free to do with it whatever you want < / B> *

this class is an abstract class, to mak e sure you do not create any new instances of it. It does not throw any exceptions and * the code is much more 'C' like than pure object oriented. There are no implemented interfaces and no inheritance in use. In fact, it * is written as close as possible to the original JavaScript code. I did not test tweaking the instance variables but if you do change * them, make sure to apply the same change in the 'sha1.js' library or you will not get the Same Encrypted strings. * 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.or> for Details. *

Author: TNSilverman (CTXM - 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 where you will find reference to all of the games code is buy {private abstract class sha1 {private Static firm Boolean HEXCase = false; / * HEX OUTPUT FORMAT. FALSE - LOW Ercase; True - Uppercase * / private static final string b64pad = "="; / * base-64 pad character. "=" for strict rfc compliance * / private static final int chrs = 8; / * bits per input character. 8 - ASCII; 16 - Unicode * // ** * this is one of the functions you'll usually want to call * it take A string arguments and return estur ither hEX or base-64 encoded strings * creation date: (3/27 / 2004 6:05:10 PM) * @Author tnsilverman * @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 Usually Want To Call * It Take A String Argument and returns Either Hex Or Base-64 Encoded Strings * Creation Date: (3/27/2004 6:05:10 PM) * @Author tnsilverman * @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 * code Date: (3/27/2004 6:05:10 PM ) * @author TNSilverman * @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 < 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);

} / ** * Convert An Array of Big-endian Words to a hex string. * Creation Date: (3/27/2004 6:05:10 PM) * @Author tnsilverman * @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 > 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 * code Date: (3/27/2004 6:05:10 pm) * @author Tnsilverman * @version 1.0.0 * @Return java.lang.string * @Param bin int [] * / private static string binb2str (int [] bin) {string str = "" "" "" "" "" "" "" "" "" "" "" "" "" " ) - 1; for (int i = 0; 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 TN 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 tnsilverman * @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 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));} / ** * Makes an int Array of a Length Less Than 16 An Array of Aray of Length 16 with all previous * cells at their previous indexes. * Creation Date: (3/27/2004 6:05:10 PM) * @Author tnsilverman * @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

Oldbin.Length) RetVal [I] = Oldbin [I]; Else Retval [I] = newbin [i - oldbin.length];} return retval;} / ** * Calculate the HMAC-Sha1 of a key and some data * Creation date: (3/26/2004 1:05:01 pm) * @Author tnsilverman * @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 (str2bin) 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 (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 Dat E: (3/26/2004 1:05:01 pm) * @Author tnsilverman * @version 1.0.0 * @return int * @Param x java.lang.string [] * @Param Len int * / private static INT [] Core_SHA1 (int [] x, int LEN) {/ * 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 >> (32 - CNT));} / ** * add INTS, Wrapping 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 tnsilverman * @version 1.0.0 * @return int * @Param Num int * @Param CNT INT * / Private Static int SAFE_ADD (INT X, INT Y) {INT LSW = (INT) (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 tnsilverman * @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 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); RETURN B ^ C ^ D;} / ** * determine the appropriate additive constant for the credient ity ity * creeion date: (3/26/2004 1:05 : 01 PM) * @Author tnsilverman * @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;} / ** * This is a boolean returnig test function that exists in the sha1.js library . * If it returns 'false' Something is Wrong. * Creation Date: (3/26/2004 1:05:01 PM) * @Author tnsilverman * @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 usually want to call * It take a string arguments and returns either hex or base-64 encoded strings * Creation date: (3/26/2004 1: 05:01 pm) * @Author tnsilverman * @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 usually want to call * It take a string argument and returns either hex or base-64 encoded Strings * Creation Date: (3/26/2004 1:05:01 PM) * @Author tnsilverman * @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 () * chrsz);} / ** * Convert AN 8- Bit or 16-bit string to an array of big-endian Words * in 8-bit function, characters> 255 Have their hi-byt e Silently Ignored. * Creation Date: (3/26/2004 1:05:01 PM) * @Author tnsilverman * @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 = "; INT MASK = (1 << chrsz) - 1; for (int i = 0; i > 5] | = ((int) (str .charat (i / chrsz)) & mask << (24 - i% 32);


New Post(0)