CTXM is a software house and IT services outsourcing provider.
CTXM development centers are located in Riga, Latvia and Minsk, Belarus.
CTXM is a subsidiary of Com-Tec-Co SA. from Luxembourg.
CTXM is certified by Lloyd's Register Quality Assurance as complying with ISO 9001:2001.

Class SHA1

java.lang.Object
  |
  +--SHA1

public abstract class SHA1
extends java.lang.Object

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

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.

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

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.


Field Summary
private static java.lang.String b64pad
           
private static int chrsz
           
private static boolean hexcase
           
 
Constructor Summary
SHA1()
           
 
Method Summary
static java.lang.String b64_hmac_sha1(java.lang.String key, java.lang.String data)
          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)
static java.lang.String b64_sha1(java.lang.String s)
          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)
private static java.lang.String binb2b64(int[] binarray)
          Convert an array of big-endian words to a base-64 string Creation date: (3/27/2004 6:05:10 PM)
private static java.lang.String binb2hex(int[] binarray)
          Convert an array of big-endian words to a hex string.
private static java.lang.String binb2str(int[] bin)
          Convert an array of big-endian words to a string Creation date: (3/27/2004 6:05:10 PM)
private static int bit_rol(int num, int cnt)
          Bitwise rotate a 32-bit number to the left.
private static java.lang.String cleanB64Str(java.lang.String str)
          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.
private static int[] complete216(int[] oldbin)
          Makes an int array of a length less than 16 an array of length 16 with all previous cells at their previous indexes.
private static int[] concat(int[] oldbin, int[] newbin)
          Joins two int arrays and return one that contains all the previous values.
private static int[] core_hmac_sha1(java.lang.String key, java.lang.String data)
          Calculate the HMAC-SHA1 of a key and some data Creation date: (3/26/2004 1:05:01 PM)
private static int[] core_sha1(int[] x, int len)
          Calculate the SHA-1 of an array of big-endian words, and a bit length Creation date: (3/26/2004 1:05:01 PM)
private static void doTest()
          Just a test function to output the results of the 6 working funcions to the standard out.
static java.lang.String hex_hmac_sha1(java.lang.String key, java.lang.String data)
          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)
static java.lang.String hex_sha1(java.lang.String s)
          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)
private static int rol(int num, int cnt)
          Bitwise rotate a 32-bit number to the left.
private static int safe_add(int x, int y)
          Add ints, wrapping at 2^32.
private static int sha1_ft(int t, int b, int c, int d)
          Perform the appropriate triplet combination function for the current Creation date: (3/26/2004 1:05:01 PM)
private static int sha1_kt(int t)
          Determine the appropriate additive constant for the current iteration Creation date: (3/26/2004 1:05:01 PM)
private static boolean sha1_vm_test()
          This is a boolean returnig test function that exists in the sha1.js library.
static java.lang.String str_hmac_sha1(java.lang.String key, java.lang.String data)
          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)
static java.lang.String str_sha1(java.lang.String s)
          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)
private static int[] str2binb(java.lang.String str)
          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.
private static int[] strechBinArray(int[] oldbin, int size)
          increase an int array to a desired sized + 1 while keeping the old values.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

hexcase

private static final boolean hexcase

b64pad

private static final java.lang.String b64pad

chrsz

private static final int chrsz
Constructor Detail

SHA1

public SHA1()
Method Detail

b64_hmac_sha1

public static java.lang.String b64_hmac_sha1(java.lang.String key,
                                             java.lang.String data)
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)
Parameters:
key - java.lang.String
data - java.lang.String
Returns:
java.lang.String

b64_sha1

public static java.lang.String b64_sha1(java.lang.String s)
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)
Parameters:
s - java.lang.String
Returns:
java.lang.String

binb2b64

private static java.lang.String binb2b64(int[] binarray)
Convert an array of big-endian words to a base-64 string Creation date: (3/27/2004 6:05:10 PM)
Parameters:
binarray - int[]
Returns:
java.lang.String

binb2hex

private static java.lang.String binb2hex(int[] binarray)
Convert an array of big-endian words to a hex string. Creation date: (3/27/2004 6:05:10 PM)
Parameters:
binarray - int[]
Returns:
java.lang.String

binb2str

private static java.lang.String binb2str(int[] bin)
Convert an array of big-endian words to a string Creation date: (3/27/2004 6:05:10 PM)
Parameters:
bin - int[]
Returns:
java.lang.String

bit_rol

private static int bit_rol(int num,
                           int cnt)
Bitwise rotate a 32-bit number to the left. Creation date: (3/26/2004 1:05:01 PM)
Parameters:
num - int
cnt - int
Returns:
int

cleanB64Str

private static java.lang.String cleanB64Str(java.lang.String str)
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)
Parameters:
str - java.lang.String
Returns:
java.lang.String

complete216

private static int[] complete216(int[] oldbin)
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)
Parameters:
str - java.lang.String
Returns:
int[]

concat

private static int[] concat(int[] oldbin,
                            int[] 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)
Parameters:
str - java.lang.String
Returns:
int[]

core_hmac_sha1

private static int[] core_hmac_sha1(java.lang.String key,
                                    java.lang.String data)
Calculate the HMAC-SHA1 of a key and some data Creation date: (3/26/2004 1:05:01 PM)
Parameters:
x - java.lang.String[]
len - int
Returns:
int

core_sha1

private static int[] core_sha1(int[] x,
                               int len)
Calculate the SHA-1 of an array of big-endian words, and a bit length Creation date: (3/26/2004 1:05:01 PM)
Parameters:
x - java.lang.String[]
len - int
Returns:
int

doTest

private static void doTest()
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)
Returns:
java.lang.String

hex_hmac_sha1

public static java.lang.String hex_hmac_sha1(java.lang.String key,
                                             java.lang.String data)
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)
Parameters:
key - java.lang.String
data - java.lang.String
Returns:
java.lang.String

hex_sha1

public static java.lang.String hex_sha1(java.lang.String s)
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)
Parameters:
s - java.lang.String
Returns:
java.lang.String

rol

private static int rol(int num,
                       int cnt)
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)
Parameters:
num - int
cnt - int
Returns:
int

safe_add

private static int safe_add(int x,
                            int y)
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)
Parameters:
num - int
cnt - int
Returns:
int

sha1_ft

private static int sha1_ft(int t,
                           int b,
                           int c,
                           int d)
Perform the appropriate triplet combination function for the current Creation date: (3/26/2004 1:05:01 PM)
Parameters:
t - int
b - int
c - int
d - int
Returns:
int

sha1_kt

private static int sha1_kt(int t)
Determine the appropriate additive constant for the current iteration Creation date: (3/26/2004 1:05:01 PM)
Parameters:
t - int
Returns:
int

sha1_vm_test

private static boolean sha1_vm_test()
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)
Parameters:
s - java.lang.String
Returns:
java.lang.String

str_hmac_sha1

public static java.lang.String str_hmac_sha1(java.lang.String key,
                                             java.lang.String data)
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)
Parameters:
key - java.lang.String
data - java.lang.String
Returns:
java.lang.String

str_sha1

public static java.lang.String str_sha1(java.lang.String s)
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)
Parameters:
s - java.lang.String
Returns:
java.lang.String

str2binb

private static int[] str2binb(java.lang.String str)
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)
Parameters:
str - java.lang.String
Returns:
int[]

strechBinArray

private static int[] strechBinArray(int[] oldbin,
                                    int size)
increase an int array to a desired sized + 1 while keeping the old values. Creation date: (3/26/2004 1:05:01 PM)
Parameters:
str - java.lang.String
Returns:
int[]

XG Project

T.N.Silverman, the author of this code, is the Project Manager of the XG project in CTXM. The people in this project produce client/server Flash/J2EE games for the Sport betting industry.