Hi list,

Im porting a SHA-1 implementation from java to AS3 and theres a strange
behaviour that I cant deal with, I really dont know if this is a Java or a
Flash question, but here it goes.


JAVA

tracing "int " + blk + " + " + 0x5A827999 + " + " + ro + " + " + prev+ " = "
+ data[z]

output int -67371266 + 1518500249 + 585074272 + 271733878 = -1987030163


FLEX
tracing "int " + blk + " + " + 0x5A827999 + " + " + ro + " + " + prev+ " = "
+ data[z]

output int -67371266 + 1518500249 + 585074272 + 271733878 = 2307937133

I know some arithematics enough to know that a first glance Flex is dealing
right, but since this implementation work with Java I reall whant to know
whats wrongh here.

this is happening in the R0 function.


Thankz in advance



Heres the Java source

/*
* SHA1.java - An implementation of the SHA-1 Algorithm
*
* This version by Chuck McManis ([EMAIL PROTECTED]) and
* still public domain.
*
* Adapted for JabberApplet by M.Kiesel
* This is needed because Java security bugs in some Netscape
* versions prevent us from using the SHA1 routines that are
* built-in in Java :-(
*
* Based on the C code that Steve Reid wrote his header
* was :
* SHA-1 in C
* By Steve Reid <[EMAIL PROTECTED]>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/



import java.util.*;

/**
* This is a simple port of Steve Reid's SHA-1 code into Java.
* I've run his test vectors through the code and they all pass.
*
*/
public final class SHA1 {
private int state[] = new int[5];
private long count;
public byte[] digestBits;
public boolean digestValid;


public SHA1() {
state = new int[5];
count = 0;
if (block == null)
block = new int[16];
digestBits = new byte[20];
digestValid = false;
}

/**
* Add specific bytes to the digest.
*/
public synchronized void update(byte input[], int offset, int len) {
for (int i = 0; i < len; i++) {
update(input[i+offset]);
}
}

/**
* Add an array of bytes to the digest.
*/
public synchronized void update(byte input[]) {
update(input, 0, input.length);
}

/**
* Treat the string as a sequence of ISO-Latin1 (8 bit) characters.
*/
public void updateASCII(String input) {
int i, len;
byte x;

len = input.length();
for (i = 0; i < len; i++) {
x = (byte) (input.charAt(i) & 0xff);
update(x);
}
}

/*
* The following array forms the basis for the transform
* buffer. Update puts bytes into this buffer and then
* transform adds it into the state of the digest.
*/
private int block[] = new int[16];
private int blockIndex;

/*
* These functions are taken out of #defines in Steve's
* code. Java doesn't have a preprocessor so the first
* step is to just promote them to real methods.
* Later we can optimize them out into inline code,
* note that by making them final some compilers will
* inline them when given the -O flag.
*/
final int rol(int value, int bits) {
int q = (value << bits) | (value >>> (32 - bits));
return q;
}

final int blk0(int i) {
block[i] = (rol(block[i],24)&0xFF00FF00) | (rol(block[i],8)&0x00FF00FF);
//System.out.println("==" + 0xFF00FF00);

return block[i];
}

final int blk(int i) {
block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15]^
block[(i+2)&15]^block[i&15], 1);
return (block[i&15]);
}

final void R0(int data[], int v, int w, int x , int y, int z, int i) {

int tmp = data[x] ^ data[y] ;
int tmp2 = data[w] & tmp;
int tmp3 = tmp2 ^ data[y];
int blk = tmp3 + blk0(i);
int ro = rol(data[v] ,5);

int prev = data[z];
data[z] += blk + 0x5A827999 + ro;
System.out.println("eq " + blk + " + " + 0x5A827999 + " + " + ro + " + " +
prev+ " = " + data[z]);
data[w] = rol(data[w], 30);
//System.out.println("==" + 0x5A827999);
}

final void R1(int data[], int v, int w, int x, int y, int z, int i) {
data[z] += ((data[w] & (data[x] ^ data[y])) ^ data[y]) +
blk(i) + 0x5A827999 + rol(data[v] ,5);
data[w] = rol(data[w], 30);
}

final void R2(int data[], int v, int w, int x, int y, int z, int i) {
data[z] += (data[w] ^ data[x] ^ data[y]) +
blk(i) + 0x6ED9EBA1 + rol(data[v] ,5);
data[w] = rol(data[w], 30);

}

final void R3(int data[], int v, int w, int x, int y, int z, int i) {
data[z] += (((data[w] | data[x]) & data[y]) | (data[w] & data[x])) +
blk(i) + 0x8F1BBCDC + rol(data[v] ,5);
data[w] = rol(data[w], 30);

}

final void R4(int data[], int v, int w, int x, int y, int z, int i) {
data[z] += (data[w] ^ data[x] ^ data[y]) +
blk(i) + 0xCA62C1D6 + rol(data[v] ,5);
data[w] = rol(data[w], 30);
//System.out.println("==" + 0xCA62C1D6);
}


/*
* Steve's original code and comments :
*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*
* #define blk0(i) block->l[i]
* #define blk(i) (block->l[i&15] =
rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
* ^block->l[(i+2)&15]^block->l[i&15],1))
*
* (R0+R1), R2, R3, R4 are the different operations used in SHA1
* #define R0(v,w,x,y,z,i)
z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
* #define R1(v,w,x,y,z,i)
z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
* #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
* #define R3(v,w,x,y,z,i)
z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
* #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
*/

int dd[] = new int[5];

/**
* Hash a single 512-bit block. This is the core of the algorithm.
*
* Note that working with arrays is very inefficent in Java as it
* does a class cast check each time you store into the array.
*
*/

void transform() {

/* Copy context->state[] to working vars */
dd[0] = state[0];
dd[1] = state[1];
dd[2] = state[2];
dd[3] = state[3];
dd[4] = state[4];


/* 4 rounds of 20 operations each. Loop unrolled. */
R0(dd,0,1,2,3,4, 0);

R0(dd,4,0,1,2,3, 1);
R0(dd,3,4,0,1,2, 2);
R0(dd,2,3,4,0,1, 3);
System.out.println("transform " + dd[0]);
System.out.println("transform " + dd[1]);
System.out.println("transform " + dd[2]);
System.out.println("transform " + dd[3]);
System.out.println("transform " + dd[4]);

R0(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2,57);
R3(dd,2,3,4,0,1,58);
R3(dd,1,2,3,4,0,59);
R4(dd,0,1,2,3,4,60);
R4(dd,4,0,1,2,3,61);
R4(dd,3,4,0,1,2,62);
R4(dd,2,3,4,0,1,63);
R4(dd,1,2,3,4,0,64);
R4(dd,0,1,2,3,4,65);
R4(dd,4,0,1,2,3,66);
R4(dd,3,4,0,1,2,67);
R4(dd,2,3,4,0,1,68);
R4(dd,1,2,3,4,0,69);
R4(dd,0,1,2,3,4,70);
R4(dd,4,0,1,2,3,71);
R4(dd,3,4,0,1,2,72);
R4(dd,2,3,4,0,1,73);
R4(dd,1,2,3,4,0,74);
R4(dd,0,1,2,3,4,75);
R4(dd,4,0,1,2,3,76);
R4(dd,3,4,0,1,2,77);
R4(dd,2,3,4,0,1,78);
R4(dd,1,2,3,4,0,79);
/* Add the working vars back into context.state[] */
state[0] += dd[0];
state[1] += dd[1];
state[2] += dd[2];
state[3] += dd[3];
state[4] += dd[4];
}


/**
*
* SHA1Init - Initialize new context
*/
public void init() {
/* SHA1 initialization constants */
state[0] = 0x67452301;
state[1] = 0xEFCDAB89;
state[2] = 0x98BADCFE;
state[3] = 0x10325476;
state[4] = 0xC3D2E1F0;
count = 0;
digestBits = new byte[20];
digestValid = false;
blockIndex = 0;
}

/**
* Add one byte to the digest. When this is implemented
* all of the abstract class methods end up calling
* this method for types other than bytes.
*/
public synchronized void update(byte b) {



int mask = (8 * (blockIndex & 3));

count += 8;
block[blockIndex >> 2] &= ~(0xff << mask);
block[blockIndex >> 2] |= (b & 0xff) << mask;
//System.out.println(b + " -> " + block[blockIndex >> 2]);
blockIndex++;
if (blockIndex == 64) {
transform();
blockIndex = 0;
}

}


/**
* Complete processing on the message digest.
*/
public void finish() {
byte bits[] = new byte[8];
int i, j;

for (i = 0; i < 8; i++) {
bits[i] = (byte)((count >>> (((7 - i) * 8))) & 0xff);
}

update((byte) 128);
while (blockIndex != 56)
update((byte) 0);
// This should cause a transform to happen.
update(bits);
for (i = 0; i < 20; i++) {
//System.out.println(state[i>>2]);
digestBits[i] = (byte)
((state[i>>2] >> ((3-(i & 3)) * 8) ) & 0xff);

// System.out.println("---->" + digestBits[i]);
}
digestValid = true;
}

/** Return a string that identifies this algorithm */
public String getAlg() {
return "SHA1";
}

/**
* Print out the digest in a form that can be easily compared
* to the test vectors.
*/
public String digout() {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 20; i++) {
char c1, c2;

c1 = (char) ((digestBits[i] >>> 4) & 0xf);
c2 = (char) (digestBits[i] & 0xf);

c1 = (char) ((c1 > 9) ? 'a' + (c1 - 10) : '0' + c1);
c2 = (char) ((c2 > 9) ? 'a' + (c2 - 10) : '0' + c2);
//System.out.println(c1);
sb.append(c1);
sb.append(c2);
/* if (((i+1) % 4) == 0)
sb.append(' '); */
}
return sb.toString();
}


/**
* This is a test program for the SHA1 algorithm. It puts
* the three test vectors through the algorithm and prints
* out the results (they should match.) Then it runs the
* MessageDigest benchmark method to see how fast it is.
* on my P133 its about 110 - 120K bytes/second.
*
* It then compares it to MD5, which is about 150K bytes/second.
*
* The reference to MoreOutputStream can be deleted. This is a
* small class that opens a window to display the results. This
* works around Symantec Cafe's tendency to shut down the DOS
* window after showing the output, and the inability to scroll
* back in a DOS window.
*/

// public static void main(String args[]) {
// int i, j;
// SHA1 s = new SHA1();

// System.out.println("SHA-1 Test PROGRAM.");
// System.out.println("This code runs the test vectors through the code.");

/* "abc"
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D */

// System.out.println("First test is 'abc'");
// String z = "abc";
// s.init();
// s.update((byte) 'a');
// s.update((byte) 'b');
// s.update((byte) 'c');
// s.finish();
// System.out.println(s.digout());
// System.out.println("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D");


/* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 */

// System.out.println("Next Test is
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'");
// z = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
// s.init();
// s.updateASCII(z);
// s.finish();
// System.out.println(s.digout());
// System.out.println("84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1");

/* A million repetitions of "a"
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */

// System.out.println("Last test is 1 million 'a' characters.");
// s.init();
// for (i = 0; i < 1000000; i++)
// s.update((byte) 'a');
// s.finish();
// System.out.println(s.digout());
// System.out.println("34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F");
// }
}



Heres the flash source

package com.mysql.security
{
/*
* SHA1.java - An implementation of the SHA-1 Algorithm
*
* This version by Chuck McManis ([EMAIL PROTECTED]) and
* still public domain.
*
* Adapted for JabberApplet by M.Kiesel
* This is needed because Java security bugs in some Netscape
* versions prevent us from using the SHA1 routines that are
* built-in in Java :-(
*
* Based on the C code that Steve Reid wrote his header
* was :
* SHA-1 in C
* By Steve Reid <[EMAIL PROTECTED]>
* 100% Public Domain
*
* Test Vectors (from FIPS PUB 180-1)
* "abc"
* A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
* 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
* A million repetitions of "a"
* 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
*/
import flash.util.StringBuilder;
import flash.util.ByteArray;
import flash.util.trace;
/**
* This is a simple port of Steve Reid's SHA-1 code into Java.
* I've run his test vectors through the code and they all pass.
*
*/
public final class SHA1
{
//array of ints new int[5];
private var state : Array = new Array (5);
//long
private var count : Number;
//byte[]
public var digestBits : Array;
public var digestValid : Boolean;
/*
* The following array forms the basis for the transform
* buffer. Update puts bytes into this buffer and then
* transform adds it into the state of the digest.
*/
//private int block[] = new int[16];
//private int blockIndex;
private var block:Array = new Array(16);
private var blockIndex:uint;
//private
//array of ints

public function SHA1 ()
{
state = new Array (5);
count = 0;
if (block == null)
{
block =new Array(16);
}

//digestBits = new byte[20];
digestBits = new Array (20);
digestValid = false;
}
/**
* Add specific bytes to the digest.
synchronized
overload its not possible so function renamed from update to updateByteArray
*/
public function updateByte (input : Array, offset : int, len : int) : Void
{
for (var i : uint = 0; i < len; i ++)
{
update(input[i]);
}
}
/**
* Add an array of bytes to the digest.
synchronized
*/
public function updateByteArray (input : Array)
{

updateByte (input, 0, input.length);



}
/**
* Treat the string as a sequence of ISO-Latin1 (8 bit) characters.
*/
public function updateASCII (input:String):Void
{
/*
int i, len;
byte x;

len = input.length();
for (i = 0; i < len; i++) {
x = (byte) (input.charAt(i) & 0xff);
update(x);
}
*/
var len : uint = input.length;
for (var i : uint = 0; i < len; i ++)
{
var x : ByteArray = new ByteArray ();
x.writeByte (input.charAt (i) & 0xff);
update(x [0]);
}
}

/*
* These functions are taken out of #defines in Steve's
* code. Java doesn't have a preprocessor so the first
* step is to just promote them to real methods.
* Later we can optimize them out into inline code,
* note that by making them final some compilers will
* inline them when given the -O flag.
*/
final function rol (value:int, bits:int):int
{
private var q:int = (value << bits) | (value >>> (32 - bits));

return q;
}

final function blk0 (i:int):int
{
/*
0xFF00FF00 -16711936
0x00FF00FF 16711935
*/
block [i] = (rol (int(block [i]) , 24) &(-16711936)) | (rol (int(block [i])
, 8) &16711935);

return block [i];
}

final function blk (i:int):int
{
block [i&15] = rol (block [(i + 13) &15] ^block [(i + 8) &15] ^
block [(i + 2) &15] ^block [i&15] , 1);

return (block [i&15]);
}

final function R0 (data:Array , v:int, w:int, x:int , y:int, z:int,
i:int):Void
{

//1518500249
// 0x5A827999
var tmp:int = data [x] ^ data [y];
var tmp2:int = data [w] & tmp;
var tmp3:int = tmp2 ^ data [y]
var blk:int = tmp3 + blk0 (i)
var ro:int = rol (int(data [v]) , 5)
var prev: int = data[z];
data [z] = data [z] + (blk + 0x5A827999 + ro);


trace("eq " + blk + " + " + 0x5A827999 + " + " + ro + " + " + prev+ " = " +
data[z])

//its a fucken int
data [w] = rol (int(data [w]) , 30);
}
final function R1 (data:Array , v:int, w:int, x:int, y:int, z:int,
i:int):Void
{
data [z] += ((data [w] & (data [x] ^ data [y])) ^ data [y]) +
blk (i) + 0x5A827999 + rol (int(data [v]) , 5);
data [w] = rol (int(data [w]) , 30);
}

final function R2 (data:Array , v:int, w:int, x:int, y:int, z:int,
i:int):Void
{
data [z] += (data [w] ^ data [x] ^ data [y]) +
blk (i) + 0x6ED9EBA1 + rol (int(data [v]) , 5);
data [w] = rol (int(data [w]) , 30);
}
final function R3 (data:Array ,v:int, w:int, x:int, y:int, z:int,
i:int):Void
{
data [z] += (((data [w] | data [x]) & data [y]) | (data [w] & data [x])) +
blk (i) + 0x8F1BBCDC + rol (int(data [v]) , 5);
data [w] = rol (int(data [w]) , 30);
}
final function R4 (data:Array ,v:int, w:int, x:int, y:int, z:int,
i:int):Void
{
data [z] += (data [w] ^ data [x] ^ data [y]) +
blk (i) + 0xCA62C1D6 + rol (int(data [v]) , 5);
data [w] = rol (int(data [w]) , 30);
}
/*
* Steve's original code and comments :
*
* blk0() and blk() perform the initial expand.
* I got the idea of expanding during the round function from SSLeay
*
* #define blk0(i) block->l[i]
* #define blk(i) (block->l[i&15] =
rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
* ^block->l[(i+2)&15]^block->l[i&15],1))
*
* (R0+R1), R2, R3, R4 are the different operations used in SHA1
* #define R0(v,w,x,y,z,i)
z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
* #define R1(v,w,x,y,z,i)
z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
* #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
* #define R3(v,w,x,y,z,i)
z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
* #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
*/
private var dd:Array = new Array(5);
/**
* Hash a single 512-bit block. This is the core of the algorithm.
*
* Note that working with arrays is very inefficent in Java as it
* does a class cast check each time you store into the array.
*
*/
public function transform ():Void
{
/* Copy context->state[] to working vars */
dd [0] = state [0];
dd [1] = state [1];
dd [2] = state [2];
dd [3] = state [3];
dd [4] = state [4];

/* 4 rounds of 20 operations each. Loop unrolled. */

R0 (dd, 0, 1, 2, 3, 4, 0);

R0 (dd, 4, 0, 1, 2, 3, 1);
R0 (dd, 3, 4, 0, 1, 2, 2);
R0 (dd, 2, 3, 4, 0, 1, 3);
trace("transform " + dd[0])
trace("transform " + dd[1])
trace("transform " + dd[2])
trace("transform " + dd[3])
trace("transform " + dd[4])

R0 (dd, 1, 2, 3, 4, 0, 4);
R0 (dd, 0, 1, 2, 3, 4, 5);

R0 (dd, 4, 0, 1, 2, 3, 6);
R0 (dd, 3, 4, 0, 1, 2, 7);
R0 (dd, 2, 3, 4, 0, 1, 8);
R0 (dd, 1, 2, 3, 4, 0, 9);
R0 (dd, 0, 1, 2, 3, 4, 10);
R0 (dd, 4, 0, 1, 2, 3, 11);
R0 (dd, 3, 4, 0, 1, 2, 12);
R0 (dd, 2, 3, 4, 0, 1, 13);
R0 (dd, 1, 2, 3, 4, 0, 14);
R0 (dd, 0, 1, 2, 3, 4, 15);
R1 (dd, 4, 0, 1, 2, 3, 16);
R1 (dd, 3, 4, 0, 1, 2, 17);
R1 (dd, 2, 3, 4, 0, 1, 18);
R1 (dd, 1, 2, 3, 4, 0, 19);
R2 (dd, 0, 1, 2, 3, 4, 20);
R2 (dd, 4, 0, 1, 2, 3, 21);
R2 (dd, 3, 4, 0, 1, 2, 22);
R2 (dd, 2, 3, 4, 0, 1, 23);
R2 (dd, 1, 2, 3, 4, 0, 24);
R2 (dd, 0, 1, 2, 3, 4, 25);
R2 (dd, 4, 0, 1, 2, 3, 26);
R2 (dd, 3, 4, 0, 1, 2, 27);
R2 (dd, 2, 3, 4, 0, 1,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, 2, 3, 4, 0, 1, 68);
R4 (dd, 1, 2, 3, 4, 0, 69);
R4 (dd, 0, 1, 2, 3, 4, 70);
R4 (dd, 4, 0, 1, 2, 3, 71);
R4 (dd, 3, 4, 0, 1, 2, 72);
R4 (dd, 2, 3, 4, 0, 1, 73);
R4 (dd, 1, 2, 3, 4, 0, 74);
R4 (dd, 0, 1, 2, 3, 4, 75);
R4 (dd, 4, 0, 1, 2, 3, 76);
R4 (dd, 3, 4, 0, 1, 2, 77);
R4 (dd, 2, 3, 4, 0, 1, 78);
R4 (dd, 1, 2, 3, 4, 0, 79);
/* Add the working vars back into context.state[] */
state [0] += dd [0];
state [1] += dd [1];
state [2] += dd [2];
state [3] += dd [3];
state [4] += dd [4];
}
/**
*
* SHA1Init - Initialize new context
*/
public function init ():Void
{
/* SHA1 initialization constants */
/*
state [0] = 0x67452301;
state [1] = 0xEFCDAB89;
state [2] = 0x98BADCFE;
state [3] = 0x10325476;
state [4] = 0xC3D2E1F0;
*/

state [0] = 1732584193;
state [1] = -271733879;
state [2] = -1732584194;
state [3] = 271733878;
state [4] = -1009589776;
count = 0;
//digestBits = new byte [20];
digestBits = new Array(20);

digestValid = false;
blockIndex = 0;
}
/**
* Add one byte to the digest. When this is implemented
* all of the abstract class methods end up calling
* this method for types other than bytes.
*synchronized
*byte b
*/
public function update (b : int) : Void
{

private var mask : int = (8 * (blockIndex & 3));
count += 8;
block [blockIndex >> 2] &= ~ (0xff << mask);
block [blockIndex >> 2] |= (b & 0xff) << mask;
//trace(b + " -> " + block [blockIndex >> 2])
blockIndex ++;
if (blockIndex == 64)
{
transform ();
blockIndex = 0;
}

}
/**
* Complete processing on the message digest.
*/
public function finish () : Void
{
//byte bits [] = new byte [8];
private var bits:Array = new Array(8);
bits.length = 8

for (var j:uint = 0; j < 8; j ++)
{
bits[j] = int((count >>> (((7 - j) * 8))) & 0xff);
}
update (-128);
while (blockIndex != 56)
{
update (0);
}
// This should cause a transform to happen.
updateByteArray (bits);
trace(digestBits.toString())
for (var i:int = 0; i < 20; i ++)
{

digestBits [i] = (state [i >> 2] >> ((3 - (i & 3)) * 8)) & 0xff;
}

digestValid = true;
}
/** Return a string that identifies this algorithm */
public function getAlg () : String
{
return "SHA1";
}
/**
* Print out the digest in a form that can be easily compared
* to the test vectors.
*/
public function digout () : String
{


private var sb : StringBuilder = new StringBuilder ();
for (var i : uint = 0; i < 20; i ++)
{
//char
private var c1 : String;
private var c2 : String;

c1 = String((digestBits[i] >>> 4) & 0xf);
c2 = String(digestBits[i] & 0xf);

c1 = ((c1 > 9) ? 'a' + (c1 - 10) : '0' + c1);
c2 = ((c2 > 9) ? 'a' + (c2 - 10) : '0' + c2);
//trace(c1)
sb.append (c1);
sb.append (c2);
/* if (((i+1) % 4) == 0)
sb.append(' '); */
}
return sb.toString ();
}
/**
* This is a test program for the SHA1 algorithm. It puts
* the three test vectors through the algorithm and prints
* out the results (they should match.) Then it runs the
* MessageDigest benchmark method to see how fast it is.
* on my P133 its about 110 - 120K bytes/second.
*
* It then compares it to MD5, which is about 150K bytes/second.
*
* The reference to MoreOutputStream can be deleted. This is a
* small class that opens a window to display the results. This
* works around Symantec Cafe's tendency to shut down the DOS
* window after showing the output, and the inability to scroll
* back in a DOS window.
*/
// public static void main(String args[]) {
// int i, j;
// SHA1 s = new SHA1();
// System.out.println("SHA-1 Test PROGRAM.");
// System.out.println("This code runs the test vectors through the code.");
/* "abc"
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D */
// System.out.println("First test is 'abc'");
// String z = "abc";
// s.init();
// s.update((byte) 'a');
// s.update((byte) 'b');
// s.update((byte) 'c');
// s.finish();
// System.out.println(s.digout());
// System.out.println("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D");
/* "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 */
// System.out.println("Next Test is
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'");
// z = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
// s.init();
// s.updateASCII(z);
// s.finish();
// System.out.println(s.digout());
// System.out.println("84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1");
/* A million repetitions of "a"
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F */
// System.out.println("Last test is 1 million 'a' characters.");
// s.init();
// for (i = 0; i < 1000000; i++)
// s.update((byte) 'a');
// s.finish();
// System.out.println(s.digout());
// System.out.println("34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F");
// }

}
}






--
Cheers Tiago Janz
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to