no commit message
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* HMAC
|
||||
*
|
||||
* An ActionScript 3 implementation of HMAC, Keyed-Hashing for Message
|
||||
* Authentication, as defined by RFC-2104
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
import com.hurlant.util.Hex;
|
||||
|
||||
public class HMAC
|
||||
{
|
||||
private var hash:IHash;
|
||||
private var bits:uint;
|
||||
|
||||
/**
|
||||
* Create a HMAC object, using a Hash function, and
|
||||
* optionally a number of bits to return.
|
||||
* The HMAC will be truncated to that size if needed.
|
||||
*/
|
||||
public function HMAC(hash:IHash, bits:uint=0) {
|
||||
this.hash = hash;
|
||||
this.bits = bits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a HMAC using a key and some data.
|
||||
* It doesn't modify either, and returns a new ByteArray with the HMAC value.
|
||||
*/
|
||||
public function compute(key:ByteArray, data:ByteArray):ByteArray {
|
||||
var hashKey:ByteArray;
|
||||
if (key.length>hash.getInputSize()) {
|
||||
hashKey = hash.hash(key);
|
||||
} else {
|
||||
hashKey = new ByteArray;
|
||||
hashKey.writeBytes(key);
|
||||
}
|
||||
while (hashKey.length<hash.getInputSize()) {
|
||||
hashKey[hashKey.length]=0;
|
||||
}
|
||||
var innerKey:ByteArray = new ByteArray;
|
||||
var outerKey:ByteArray = new ByteArray;
|
||||
for (var i:uint=0;i<hashKey.length;i++) {
|
||||
innerKey[i] = hashKey[i] ^ 0x36;
|
||||
outerKey[i] = hashKey[i] ^ 0x5c;
|
||||
}
|
||||
// inner + data
|
||||
innerKey.position = hashKey.length;
|
||||
innerKey.writeBytes(data);
|
||||
var innerHash:ByteArray = hash.hash(innerKey);
|
||||
// outer + innerHash
|
||||
outerKey.position = hashKey.length;
|
||||
outerKey.writeBytes(innerHash);
|
||||
var outerHash:ByteArray = hash.hash(outerKey);
|
||||
if (bits>0 && bits<8*outerHash.length) {
|
||||
outerHash.length = bits/8;
|
||||
}
|
||||
return outerHash;
|
||||
}
|
||||
public function dispose():void {
|
||||
hash = null;
|
||||
bits = 0;
|
||||
}
|
||||
public function toString():String {
|
||||
return "hmac-"+(bits>0?bits+"-":"")+hash.toString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* IHash
|
||||
*
|
||||
* An interface for each hash function to implement
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public interface IHash
|
||||
{
|
||||
function getInputSize():uint;
|
||||
function getHashSize():uint;
|
||||
function hash(src:ByteArray):ByteArray;
|
||||
function toString():String;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* MD2
|
||||
*
|
||||
* An ActionScript 3 implementation of the RSA Data Security, Inc MD2 Message
|
||||
* Digest Algorithm, as defined in RFC 1319
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*
|
||||
* Excerpt from http://en.wikipedia.org/wiki/MD2:
|
||||
* >
|
||||
* > Rogier and Chauvaud (1997) described collisions of MD2's compression function,
|
||||
* > although they were unable to extend the attack to the full MD2.
|
||||
* >
|
||||
* > In 2004, MD2 was shown to be vulnerable to a preimage attack with time
|
||||
* > complexity equivalent to 2104 applications of the compression function
|
||||
* > (Muller, 2004).
|
||||
* > The author concludes, "MD2 can no longer be considered a secure one-way
|
||||
* > hash function".
|
||||
*
|
||||
* also, this implementaton is quite slow.
|
||||
*/
|
||||
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class MD2 implements IHash
|
||||
{
|
||||
public static const HASH_SIZE:int = 16;
|
||||
|
||||
private static const S:Array = [ // PI Digits
|
||||
41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6, 19,
|
||||
98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188, 76, 130, 202,
|
||||
30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24, 138, 23, 229, 18,
|
||||
190, 78, 196, 214, 218, 158, 222, 73, 160, 251, 245, 142, 187, 47, 238, 122,
|
||||
169, 104, 121, 145, 21, 178, 7, 63, 148, 194, 16, 137, 11, 34, 95, 33,
|
||||
128, 127, 93, 154, 90, 144, 50, 39, 53, 62, 204, 231, 191, 247, 151, 3,
|
||||
255, 25, 48, 179, 72, 165, 181, 209, 215, 94, 146, 42, 172, 86, 170, 198,
|
||||
79, 184, 56, 210, 150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241,
|
||||
69, 157, 112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2,
|
||||
27, 96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
|
||||
85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197, 234, 38,
|
||||
44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65, 129, 77, 82,
|
||||
106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123, 8, 12, 189, 177, 74,
|
||||
120, 136, 149, 139, 227, 99, 232, 109, 233, 203, 213, 254, 59, 0, 29, 57,
|
||||
242, 239, 183, 14, 102, 88, 208, 228, 166, 119, 114, 248, 235, 117, 75, 10,
|
||||
49, 68, 80, 180, 143, 237, 31, 26, 219, 153, 141, 51, 159, 17, 131, 20 ];
|
||||
|
||||
public function getInputSize():uint
|
||||
{
|
||||
return 16;
|
||||
}
|
||||
|
||||
public function getHashSize():uint
|
||||
{
|
||||
return HASH_SIZE;
|
||||
}
|
||||
|
||||
public function hash(src:ByteArray):ByteArray
|
||||
{
|
||||
// 3.1 Step 1. Padding
|
||||
var i:uint = (16-src.length%16) || 16;
|
||||
do {
|
||||
src[src.length]=i;
|
||||
} while (src.length%16!=0);
|
||||
|
||||
// 3.2 Step 2. Checksum
|
||||
var len:uint = src.length;
|
||||
var checksum:ByteArray = new ByteArray;
|
||||
var L:uint = 0;
|
||||
for (i = 0;i<len;i+=16) {
|
||||
for (var j:uint=0;j<16;j++) {
|
||||
L = checksum[j] ^= S[src[i+j] ^ L];
|
||||
}
|
||||
}
|
||||
src.position = src.length;
|
||||
src.writeBytes(checksum);
|
||||
len += 16;
|
||||
|
||||
// 3.3 Step 3. MD Buffer
|
||||
var X:ByteArray = new ByteArray;
|
||||
|
||||
// 3.4 Process Message
|
||||
for (i=0;i<len;i+=16) {
|
||||
|
||||
/* Copy block i into X */
|
||||
for (j=0;j<16;j++) {
|
||||
X[32+j] = (X[16+j] = src[i+j])^X[j];
|
||||
}
|
||||
var t:uint=0;
|
||||
/* Do 18 rounds */
|
||||
for (j=0;j<18;j++) {
|
||||
/* Round j. */
|
||||
for (var k:uint=0;k<48;k++) {
|
||||
X[k] = t = X[k]^S[t];
|
||||
}
|
||||
t = (t+j)&0xff;
|
||||
}
|
||||
}
|
||||
// 3.5 Step 5. Output
|
||||
X.length = 16;
|
||||
return X;
|
||||
}
|
||||
|
||||
public function toString():String
|
||||
{
|
||||
return "md2";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* MD5
|
||||
*
|
||||
* An ActionScript 3 implementation of the RSA Data Security, Inc. MD5 Message
|
||||
* Digest Algorithm, as defined in RFC 1321.
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* Derived from
|
||||
* A JavaScript implementation of the same.
|
||||
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
|
||||
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class MD5 implements IHash
|
||||
{
|
||||
public static const HASH_SIZE:int = 16;
|
||||
|
||||
public function getInputSize():uint
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public function getHashSize():uint
|
||||
{
|
||||
return HASH_SIZE;
|
||||
}
|
||||
|
||||
public function hash(src:ByteArray):ByteArray
|
||||
{
|
||||
var len:uint = src.length *8;
|
||||
// pad to nearest int.
|
||||
while (src.length%4!=0) {
|
||||
src[src.length]=0;
|
||||
}
|
||||
// convert ByteArray to an array of uint
|
||||
src.position=0;
|
||||
var a:Array = [];
|
||||
src.endian="littleEndian";
|
||||
for (var i:uint=0;i<src.length;i+=4) {
|
||||
a.push(src.readUnsignedInt());
|
||||
}
|
||||
var h:Array = core_md5(a, len);
|
||||
var out:ByteArray = new ByteArray;
|
||||
out.endian="littleEndian";
|
||||
for (i=0;i<4;i++) {
|
||||
out.writeUnsignedInt(h[i]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private function core_md5(x:Array, len:uint):Array {
|
||||
/* append padding */
|
||||
x[len >> 5] |= 0x80 << ((len) % 32);
|
||||
x[(((len + 64) >>> 9) << 4) + 14] = len;
|
||||
|
||||
var a:uint = 0x67452301; // 1732584193;
|
||||
var b:uint = 0xEFCDAB89; //-271733879;
|
||||
var c:uint = 0x98BADCFE; //-1732584194;
|
||||
var d:uint = 0x10325476; // 271733878;
|
||||
|
||||
for(var i:uint = 0; i < x.length; i += 16)
|
||||
{
|
||||
x[i]||=0; x[i+1]||=0; x[i+2]||=0; x[i+3]||=0;
|
||||
x[i+4]||=0; x[i+5]||=0; x[i+6]||=0; x[i+7]||=0;
|
||||
x[i+8]||=0; x[i+9]||=0; x[i+10]||=0; x[i+11]||=0;
|
||||
x[i+12]||=0; x[i+13]||=0; x[i+14]||=0; x[i+15]||=0;
|
||||
|
||||
var olda:uint = a;
|
||||
var oldb:uint = b;
|
||||
var oldc:uint = c;
|
||||
var oldd:uint = d;
|
||||
|
||||
a = ff(a, b, c, d, x[i+ 0], 7 , 0xD76AA478);
|
||||
d = ff(d, a, b, c, x[i+ 1], 12, 0xE8C7B756);
|
||||
c = ff(c, d, a, b, x[i+ 2], 17, 0x242070DB);
|
||||
b = ff(b, c, d, a, x[i+ 3], 22, 0xC1BDCEEE);
|
||||
a = ff(a, b, c, d, x[i+ 4], 7 , 0xF57C0FAF);
|
||||
d = ff(d, a, b, c, x[i+ 5], 12, 0x4787C62A);
|
||||
c = ff(c, d, a, b, x[i+ 6], 17, 0xA8304613);
|
||||
b = ff(b, c, d, a, x[i+ 7], 22, 0xFD469501);
|
||||
a = ff(a, b, c, d, x[i+ 8], 7 , 0x698098D8);
|
||||
d = ff(d, a, b, c, x[i+ 9], 12, 0x8B44F7AF);
|
||||
c = ff(c, d, a, b, x[i+10], 17, 0xFFFF5BB1);
|
||||
b = ff(b, c, d, a, x[i+11], 22, 0x895CD7BE);
|
||||
a = ff(a, b, c, d, x[i+12], 7 , 0x6B901122);
|
||||
d = ff(d, a, b, c, x[i+13], 12, 0xFD987193);
|
||||
c = ff(c, d, a, b, x[i+14], 17, 0xA679438E);
|
||||
b = ff(b, c, d, a, x[i+15], 22, 0x49B40821);
|
||||
|
||||
a = gg(a, b, c, d, x[i+ 1], 5 , 0xf61e2562);
|
||||
d = gg(d, a, b, c, x[i+ 6], 9 , 0xc040b340);
|
||||
c = gg(c, d, a, b, x[i+11], 14, 0x265e5a51);
|
||||
b = gg(b, c, d, a, x[i+ 0], 20, 0xe9b6c7aa);
|
||||
a = gg(a, b, c, d, x[i+ 5], 5 , 0xd62f105d);
|
||||
d = gg(d, a, b, c, x[i+10], 9 , 0x2441453);
|
||||
c = gg(c, d, a, b, x[i+15], 14, 0xd8a1e681);
|
||||
b = gg(b, c, d, a, x[i+ 4], 20, 0xe7d3fbc8);
|
||||
a = gg(a, b, c, d, x[i+ 9], 5 , 0x21e1cde6);
|
||||
d = gg(d, a, b, c, x[i+14], 9 , 0xc33707d6);
|
||||
c = gg(c, d, a, b, x[i+ 3], 14, 0xf4d50d87);
|
||||
b = gg(b, c, d, a, x[i+ 8], 20, 0x455a14ed);
|
||||
a = gg(a, b, c, d, x[i+13], 5 , 0xa9e3e905);
|
||||
d = gg(d, a, b, c, x[i+ 2], 9 , 0xfcefa3f8);
|
||||
c = gg(c, d, a, b, x[i+ 7], 14, 0x676f02d9);
|
||||
b = gg(b, c, d, a, x[i+12], 20, 0x8d2a4c8a);
|
||||
|
||||
a = hh(a, b, c, d, x[i+ 5], 4 , 0xfffa3942);
|
||||
d = hh(d, a, b, c, x[i+ 8], 11, 0x8771f681);
|
||||
c = hh(c, d, a, b, x[i+11], 16, 0x6d9d6122);
|
||||
b = hh(b, c, d, a, x[i+14], 23, 0xfde5380c);
|
||||
a = hh(a, b, c, d, x[i+ 1], 4 , 0xa4beea44);
|
||||
d = hh(d, a, b, c, x[i+ 4], 11, 0x4bdecfa9);
|
||||
c = hh(c, d, a, b, x[i+ 7], 16, 0xf6bb4b60);
|
||||
b = hh(b, c, d, a, x[i+10], 23, 0xbebfbc70);
|
||||
a = hh(a, b, c, d, x[i+13], 4 , 0x289b7ec6);
|
||||
d = hh(d, a, b, c, x[i+ 0], 11, 0xeaa127fa);
|
||||
c = hh(c, d, a, b, x[i+ 3], 16, 0xd4ef3085);
|
||||
b = hh(b, c, d, a, x[i+ 6], 23, 0x4881d05);
|
||||
a = hh(a, b, c, d, x[i+ 9], 4 , 0xd9d4d039);
|
||||
d = hh(d, a, b, c, x[i+12], 11, 0xe6db99e5);
|
||||
c = hh(c, d, a, b, x[i+15], 16, 0x1fa27cf8);
|
||||
b = hh(b, c, d, a, x[i+ 2], 23, 0xc4ac5665);
|
||||
|
||||
a = ii(a, b, c, d, x[i+ 0], 6 , 0xf4292244);
|
||||
d = ii(d, a, b, c, x[i+ 7], 10, 0x432aff97);
|
||||
c = ii(c, d, a, b, x[i+14], 15, 0xab9423a7);
|
||||
b = ii(b, c, d, a, x[i+ 5], 21, 0xfc93a039);
|
||||
a = ii(a, b, c, d, x[i+12], 6 , 0x655b59c3);
|
||||
d = ii(d, a, b, c, x[i+ 3], 10, 0x8f0ccc92);
|
||||
c = ii(c, d, a, b, x[i+10], 15, 0xffeff47d);
|
||||
b = ii(b, c, d, a, x[i+ 1], 21, 0x85845dd1);
|
||||
a = ii(a, b, c, d, x[i+ 8], 6 , 0x6fa87e4f);
|
||||
d = ii(d, a, b, c, x[i+15], 10, 0xfe2ce6e0);
|
||||
c = ii(c, d, a, b, x[i+ 6], 15, 0xa3014314);
|
||||
b = ii(b, c, d, a, x[i+13], 21, 0x4e0811a1);
|
||||
a = ii(a, b, c, d, x[i+ 4], 6 , 0xf7537e82);
|
||||
d = ii(d, a, b, c, x[i+11], 10, 0xbd3af235);
|
||||
c = ii(c, d, a, b, x[i+ 2], 15, 0x2ad7d2bb);
|
||||
b = ii(b, c, d, a, x[i+ 9], 21, 0xeb86d391);
|
||||
|
||||
a += olda;
|
||||
b += oldb;
|
||||
c += oldc;
|
||||
d += oldd;
|
||||
|
||||
}
|
||||
return [ a, b, c, d ];
|
||||
}
|
||||
|
||||
/*
|
||||
* Bitwise rotate a 32-bit number to the left.
|
||||
*/
|
||||
private function rol(num:uint, cnt:uint):uint
|
||||
{
|
||||
return (num << cnt) | (num >>> (32 - cnt));
|
||||
}
|
||||
|
||||
/*
|
||||
* These functions implement the four basic operations the algorithm uses.
|
||||
*/
|
||||
private function cmn(q:uint, a:uint, b:uint, x:uint, s:uint, t:uint):uint {
|
||||
return rol(a + q + x + t, s) + b;
|
||||
}
|
||||
private function ff(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
|
||||
return cmn((b & c) | ((~b) & d), a, b, x, s, t);
|
||||
}
|
||||
private function gg(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
|
||||
return cmn((b & d) | (c & (~d)), a, b, x, s, t);
|
||||
}
|
||||
private function hh(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
|
||||
return cmn(b ^ c ^ d, a, b, x, s, t);
|
||||
}
|
||||
private function ii(a:uint, b:uint, c:uint, d:uint, x:uint, s:uint, t:uint):uint {
|
||||
return cmn(c ^ (b | (~d)), a, b, x, s, t);
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return "md5";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* SHA1
|
||||
*
|
||||
* An ActionScript 3 implementation of Secure Hash Algorithm, SHA-1, as defined
|
||||
* in FIPS PUB 180-1
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* Derived from:
|
||||
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
|
||||
* in FIPS PUB 180-1
|
||||
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
|
||||
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class SHA1 extends SHABase implements IHash
|
||||
{
|
||||
public static const HASH_SIZE:int = 20;
|
||||
|
||||
public override function getHashSize():uint {
|
||||
return HASH_SIZE;
|
||||
}
|
||||
|
||||
protected override function core(x:Array, len:uint):Array
|
||||
{
|
||||
/* append padding */
|
||||
x[len >> 5] |= 0x80 << (24 - len % 32);
|
||||
x[((len + 64 >> 9) << 4) + 15] = len;
|
||||
|
||||
var w:Array = [];
|
||||
var a:uint = 0x67452301; //1732584193;
|
||||
var b:uint = 0xEFCDAB89; //-271733879;
|
||||
var c:uint = 0x98BADCFE; //-1732584194;
|
||||
var d:uint = 0x10325476; //271733878;
|
||||
var e:uint = 0xC3D2E1F0; //-1009589776;
|
||||
|
||||
for(var i:uint = 0; i < x.length; i += 16)
|
||||
{
|
||||
|
||||
var olda:uint = a;
|
||||
var oldb:uint = b;
|
||||
var oldc:uint = c;
|
||||
var oldd:uint = d;
|
||||
var olde:uint = e;
|
||||
|
||||
for(var j:uint = 0; j < 80; j++)
|
||||
{
|
||||
if (j < 16) {
|
||||
w[j] = x[i + j] || 0;
|
||||
} else {
|
||||
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
|
||||
}
|
||||
var t:uint = rol(a,5) + ft(j,b,c,d) + e + w[j] + kt(j);
|
||||
e = d;
|
||||
d = c;
|
||||
c = rol(b, 30);
|
||||
b = a;
|
||||
a = t;
|
||||
}
|
||||
a += olda;
|
||||
b += oldb;
|
||||
c += oldc;
|
||||
d += oldd;
|
||||
e += olde;
|
||||
}
|
||||
return [ a, b, c, d, e ];
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Bitwise rotate a 32-bit number to the left.
|
||||
*/
|
||||
private function rol(num:uint, cnt:uint):uint
|
||||
{
|
||||
return (num << cnt) | (num >>> (32 - cnt));
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform the appropriate triplet combination function for the current
|
||||
* iteration
|
||||
*/
|
||||
private function ft(t:uint, b:uint, c:uint, d:uint):uint
|
||||
{
|
||||
if(t < 20) return (b & c) | ((~b) & d);
|
||||
if(t < 40) return b ^ c ^ d;
|
||||
if(t < 60) return (b & c) | (b & d) | (c & d);
|
||||
return b ^ c ^ d;
|
||||
}
|
||||
|
||||
/*
|
||||
* Determine the appropriate additive constant for the current iteration
|
||||
*/
|
||||
private function kt(t:uint):uint
|
||||
{
|
||||
return (t < 20) ? 0x5A827999 : (t < 40) ? 0x6ED9EBA1 :
|
||||
(t < 60) ? 0x8F1BBCDC : 0xCA62C1D6;
|
||||
}
|
||||
public override function toString():String {
|
||||
return "sha1";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* SHA224
|
||||
*
|
||||
* An ActionScript 3 implementation of Secure Hash Algorithm, SHA-224, as defined
|
||||
* in FIPS PUB 180-2
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
public class SHA224 extends SHA256
|
||||
{
|
||||
function SHA224() {
|
||||
h = [
|
||||
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
|
||||
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
|
||||
];
|
||||
}
|
||||
|
||||
public override function getHashSize():uint {
|
||||
return 28;
|
||||
}
|
||||
public override function toString():String {
|
||||
return "sha224";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* SHA256
|
||||
*
|
||||
* An ActionScript 3 implementation of Secure Hash Algorithm, SHA-256, as defined
|
||||
* in FIPS PUB 180-2
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* Derived from:
|
||||
* A JavaScript implementation of the Secure Hash Standard
|
||||
* Version 0.3 Copyright Angel Marin 2003-2004 - http://anmar.eu.org/
|
||||
* Derived from:
|
||||
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
|
||||
* in FIPS PUB 180-1
|
||||
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
|
||||
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class SHA256 extends SHABase implements IHash
|
||||
{
|
||||
protected static const k:Array = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
|
||||
protected var h:Array = [
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
||||
];
|
||||
|
||||
public function SHA256(){
|
||||
}
|
||||
|
||||
public override function getHashSize():uint
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
protected override function core(x:Array, len:uint):Array {
|
||||
/* append padding */
|
||||
x[len >> 5] |= 0x80 << (24 - len % 32);
|
||||
x[((len + 64 >> 9) << 4) + 15] = len;
|
||||
|
||||
var w:Array = [];
|
||||
var a:uint = h[0];
|
||||
var b:uint = h[1];
|
||||
var c:uint = h[2];
|
||||
var d:uint = h[3];
|
||||
var e:uint = h[4];
|
||||
var f:uint = h[5];
|
||||
var g:uint = h[6];
|
||||
var h:uint = h[7];
|
||||
|
||||
for (var i:uint=0; i<x.length; i+=16) {
|
||||
var olda:uint = a;
|
||||
var oldb:uint = b;
|
||||
var oldc:uint = c;
|
||||
var oldd:uint = d;
|
||||
var olde:uint = e;
|
||||
var oldf:uint = f;
|
||||
var oldg:uint = g;
|
||||
var oldh:uint = h;
|
||||
|
||||
for (var j:uint=0; j<64; j++) {
|
||||
if (j<16) {
|
||||
w[j] = x[i+j] || 0;
|
||||
} else {
|
||||
var s0:uint = rrol(w[j-15],7)^rrol(w[j-15],18)^(w[j-15]>>>3);
|
||||
var s1:uint = rrol(w[j-2], 17)^rrol(w[j-2],19)^(w[j-2]>>>10);
|
||||
w[j] = w[j-16] + s0 + w[j-7] + s1;
|
||||
}
|
||||
var t2:uint = (rrol(a,2) ^ rrol(a,13) ^ rrol(a,22)) + ((a&b) ^ (a&c) ^ (b&c));
|
||||
var t1:uint = h + (rrol(e,6) ^ rrol(e,11) ^ rrol(e,25)) + ((e&f)^(g&~e)) + k[j] + w[j]
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + t1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = t1 + t2;
|
||||
|
||||
}
|
||||
a += olda;
|
||||
b += oldb;
|
||||
c += oldc;
|
||||
d += oldd;
|
||||
e += olde;
|
||||
f += oldf;
|
||||
g += oldg;
|
||||
h += oldh;
|
||||
}
|
||||
return [ a,b,c,d,e,f,g,h ];
|
||||
}
|
||||
|
||||
/*
|
||||
* Bitwise rotate a 32-bit number to the right.
|
||||
*/
|
||||
protected function rrol(num:uint, cnt:uint):uint {
|
||||
return (num << (32-cnt)) | (num >>> cnt);
|
||||
}
|
||||
|
||||
public override function toString():String {
|
||||
return "sha256";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* SHABase
|
||||
*
|
||||
* An ActionScript 3 abstract class for the SHA family of hash functions
|
||||
* Copyright (c) 2007 Henri Torgemane
|
||||
*
|
||||
* See LICENSE.txt for full license information.
|
||||
*/
|
||||
package com.hurlant.crypto.hash
|
||||
{
|
||||
import flash.utils.ByteArray;
|
||||
|
||||
public class SHABase implements IHash
|
||||
{
|
||||
public function getInputSize():uint
|
||||
{
|
||||
return 64;
|
||||
}
|
||||
|
||||
public function getHashSize():uint
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function hash(src:ByteArray):ByteArray
|
||||
{
|
||||
var savedLength:uint = src.length;
|
||||
var len:uint = savedLength *8;
|
||||
// pad to nearest int.
|
||||
while (src.length%4!=0) {
|
||||
src[src.length]=0;
|
||||
}
|
||||
// convert ByteArray to an array of uint
|
||||
src.position=0;
|
||||
var a:Array = [];
|
||||
for (var i:uint=0;i<src.length;i+=4) {
|
||||
a.push(src.readUnsignedInt());
|
||||
}
|
||||
var h:Array = core(a, len);
|
||||
var out:ByteArray = new ByteArray;
|
||||
var words:uint = getHashSize()/4;
|
||||
for (i=0;i<words;i++) {
|
||||
out.writeUnsignedInt(h[i]);
|
||||
}
|
||||
// unpad, to leave the source untouched.
|
||||
src.length = savedLength;
|
||||
return out;
|
||||
}
|
||||
protected function core(x:Array, len:uint):Array {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return "sha";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user