Ask a Question related to Macromedia Flash Actionscript, Design and Development.
-
SeanRenet #1
Hash in AS 2.0
So I was about to write a hash algorythm for flash and it appears brandon hall
beat me by 2 years. Since there isn't anything here, in the flash exchange or
google I thought I would post it incase anyone else needed it. So, I converted
it to AS 2.0 and here it is. To use it, supposing you had a flash mx 2004 file
with TextInput component named str2Hash, a Lable component named lbl_hash and a
Button component named btn_Hash... Your AS code would look like this:
btn_Hash.onRelease = function() {
var myHash:Hash = new Hash();
lbl_hash.text = myHash.calcSHA1(str2Hash.text);
}
Make an AS file named Hash.as with the following code and put it where the
rest of your classes are or in the same directory your movie is in
//------------------------------------------------
// SHA1 library AS 2.0
// Converted Brandon Hall's SHA1 library to AS 2.0 by
// Sean Renet
// [email]sean@broadcastdynamcis.com[/email]
// February, 2004
//------------------------------------------------
//------------------------------------------------
// SHA1 library
// Branden J. Hall
// Fig Leaf Software
// June, 2002
//------------------------------------------------
// An implementation of the SHA-1 secure hash
//
// Based on code JavaScript code
// Copyright (C) Paul Johnston 2000 - 2002.
// Licensed under the GNU L-GPL
//------------------------------------------------
class Hash extends Math {
var hex_chr:String;
function Hash() {
hex_chr = "0123456789abcdef";
}
/*
* Take a string and return the hex representation of its SHA-1.
*/
public function calcSHA1(str:String):String {
var x = str2blks(str);
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i<x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j<80; j++) {
if (j<16) {
w[j] = x[i+j];
} else {
w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
}
var t = safe_add(safe_add(rol(a, 5), ft(j, b, c, d)), safe_add(safe_add(e,
w[j]), kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return hex(a)+hex(b)+hex(c)+hex(d)+hex(e);
}
// end calcSHA1
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the SHA1 standard.
*/
private function str2blks(str:String):Array {
// split the str into an array of its characters
var strA = str.split('');
var nblk = ((str.length+8) >> 6)+1;
var blks = new Array(nblk*16);
for (var i = 0; i<nblk*16; i++) {
blks[i] = 0;
}
for (var i = 0; i<str.length; i++) {
// now reference the character through the strA array
blks[i >> 2] |= strA[i].charCodeAt(0) << (24-(i%4)*8);
}
blks[i >> 2] |= 0x80 << (24-(i%4)*8);
blks[nblk*16-1] = str.length*8;
return blks;
}
//end str2blks
/*
* Bitwise rotate a 32-bit number to the left
*/
private function rol(num:Number, cnt:Number):Number {
return (num << cnt) | (num >>> (32-cnt));
}
//end rol
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
private function safe_add(x:Number, y:Number):Number {
var lsw = (x & 0xFFFF)+(y & 0xFFFF);
var msw = (x >> 16)+(y >> 16)+(lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
//end safe_add
/*
* Perform the appropriate triplet combination function for the current
* iteration
*/
private function ft(t:Number, b:Number, c:Number, d:Number):Number {
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;
}
//end ft
/*
* Determine the appropriate additive constant for the current iteration
*/
private function kt(t:Number):Number {
return (t<20) ? 1518500249 : (t<40) ? 1859775393 : (t<60) ? -1894007588 :
-899497514;
}
//end kt
private function hex(num:Number):String {
var str = "";
for (var j = 7; j>=0; j--) {
str += hex_chr.charAt((num >> (j*4)) & 0x0F);
}
return str;
}
//end hex
}
//end class
SeanRenet Guest
-
Hash
Hi, How set up functions hash in php4.3.2 thanks _________________________________________________________________ Charla con tus amigos en... -
hash of hash of array slices
This works Foreach ( @{$hash{$key1}{$key2}} ) This does note Foreach ( @{($hash{$key1}{$key2})} ) This gives me this error .... Can't... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Hash of Hash
Greetings, I am attempting to make a hash of hashes or something equivalent but can't seem to get it working properly. Here is what I have so... -
Another reference question (hash of hash references)
beginners, I am trying to build a hash of hash references. My problem is that I need to be able to add a key/value pair to the internal hashes......



Reply With Quote

