/**
* <b>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>
* <p><b>this class is an abstract class, to make 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 won't 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 are the only public methods. all are public and static. you have no reason to call the private ones anyway.</p></b>
* <p>the 'sha1.js' is a javascript implementation of the secure 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</p>
* <p>see <a href="http://pajhome.org.uk/crypt/md5">http://pajhome.org.uk/crypt/md5</a> for details.</p>
* <p><b>author: </b>t.n.silverman (c.t.xm - sia riga, lv) <a href="mailto:tnsilver@ctcm.com">mailto:tnsilver@ctxm.com</a>
* <br>creation date: (3/27/2004 5:57:00 pm)</p>
* <p>don't forget to visit my company, <b>ctxm</b> site at <a href="http://www.ctxm.com">http://www.ctxm.com</a> 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 compliance */
private static final int chrsz = 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 returns either hex or base-64 encoded 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 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 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 < 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 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 < binarray.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 += (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 static string 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)) & 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 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
闽公网安备 35060202000074号