Skip to content

Commit

Permalink
write zeros in one call
Browse files Browse the repository at this point in the history
  • Loading branch information
mherkender committed Apr 2, 2012
1 parent af42801 commit 5ab9f68
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions sha1.as
Expand Up @@ -12,17 +12,9 @@ package {

// sha-1 adds a 64-bit integer that has the size
// BUT enough zeros need to be added so that they'll be at the end of a chunk
var i:int = Math.ceil((originalLength + 9) / 64) * 64 - originalLength - 9;
for (; i >= 4; i -= 4) {
byteInput.writeInt(0x0);
}
if (i >= 2) {
byteInput.writeShort(0x0);
if (i === 3) {
byteInput.writeByte(0x0);
}
} else if (i === 1) {
byteInput.writeByte(0x0);
const newLen:uint = Math.ceil((originalLength + 9) / 64) * 64 - originalLength - 9;
if (newLen) {
byteInput.writeBytes(zeroByteArray, 0, newLen);
}

// append the original size of the input
Expand Down Expand Up @@ -52,8 +44,9 @@ package {
var w13:int;
var w14:int;
var w15:int;

for (i = byteInput.length >>> 6, byteInput.position = 0; i > 0; --i) {

byteInput.position = 0;
for (var i:int = byteInput.length >>> 6; i > 0; --i) {
// set up variables for this chunk
var a:int = h0;
var b:int = h1;
Expand Down Expand Up @@ -150,7 +143,7 @@ package {
h4 += e;
}

byteInput.length = 0;// cleanup
byteInput.length = 0;

var result:String = uint(h4).toString(16);
var zeros:Array = staticZeros;
Expand Down Expand Up @@ -180,3 +173,6 @@ const staticByteArray:ByteArray = new ByteArray();
staticByteArray.endian = "bigEndian";

const staticZeros:Array = ["", "0", "00", "000", "0000", "00000", "000000", "0000000"];

const zeroByteArray:ByteArray = new ByteArray();
zeroByteArray.length = 64; // enough zeros to fill one chunk

0 comments on commit 5ab9f68

Please sign in to comment.