From 5ab9f6849aed10ccbbcafaab38c35722c7994000 Mon Sep 17 00:00:00 2001 From: Maximilian Herkender Date: Sun, 1 Apr 2012 15:03:34 -0700 Subject: [PATCH] write zeros in one call --- sha1.as | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/sha1.as b/sha1.as index b01eedc..34465e8 100644 --- a/sha1.as +++ b/sha1.as @@ -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 @@ -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; @@ -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; @@ -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