fc2ブログ


ActionScript3.0でユーザーが入力したパスワードをハッシュ値に変換し保存できるのか色々調べてみました。
ActionScript3.0の知識がほとんどないので、おぼえがき程度に・・・。



Google Code as3crypto


ActionScript3.0で、MD5,SHA1,RSA、AES, DES, 3DES, BlowFishなどの暗号機能を提供する便利なライブラリです。

☆Google Code as3crypto
http://code.google.com/p/as3crypto/

☆デモサイト
http://crypto.hurlant.com/demo/


as3cryptoのダウンロード


http://code.google.com/p/as3crypto/
上記サイトの「Downloads」より「as3crypto.swc」をダウンロードします。
※SWCとは?
 SWC ファイルは、コンポーネントやその他のアセットのアーカイブファイルです。javaだとjarファイルなのかな。

実際のソース


import com.hurlant.crypto.Crypto;
import com.hurlant.crypto.hash.*;
import com.hurlant.crypto.symmetric.*;
import com.hurlant.util.Hex;
import flash.utils.ByteArray;

public class CryptoCreater {

/**
* コンストラクタ
*/
public function CryptoCreater() { }

/**
* Google as3crypto.swcを利用してハッシュ値を取得する
*
*/
public function getCryptoMD5(baseStr:String) :String{

//①受け取ったプレーンテキストをHex文字列に変換
var hexString:String;
hexString = Hex.fromString(baseStr);

//②①で生成したHex文字列をバイナリデータに変換
var bainaryString:ByteArray = new ByteArray();
bainaryString = Hex.toArray(hexString);

//③ハッシュ値生成アルゴリズムにMD5を指定
var hashString1:IHash;
hashString1 = Crypto.getHash("md5");

//④②で生成したバイナリデータと③で生成したハッシュ値を元にハッシュ値を生成
var hashString2:ByteArray;
hashString2 = hashString1.hash( bainaryString );

//④で生成したバイナリデータをプレーンテキストに変換
var resultString:String;
resultString = Hex.fromArray( hashString2 );

return resultString;
}
}

呼び出し側のソース


//入力されたパスワードをMD5ハッシュ値に変換
var crypto:CryptoCreater = new CryptoCreater();
var md5pass:String = crypto.getCryptoMD5(変換したい文字列);

結果


”actionscript”文字を変換すると”2126126ff5a3202d0ac1ec5a510b793c”になりました。
少しでも参考になれば応援お願いします。