<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="539" height="530">
<fx:Script>
<![CDATA[
/****************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2012 Adobe Systems Incorporated and it’s licensors
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
* ****************************************************************************/
import alchemy.samples.as3_crypto_wrapper;
import mx.controls.Alert;
import mx.managers.FocusManager;
import mx.utils.Base64Decoder;
import mx.utils.Base64Encoder;
private var aes128key:ByteArray = null;
private var aes128iv:ByteArray = null;
private var fm:FocusManager;
//I could possibly replace this with toString(16)
private function toBase16(input:ByteArray):String
{
var output:String = new String;
const hex:String = "0123456789abcdef";
const length:int = input.length;
for(var i:int = 0; i < length; i++) {
output += hex.charAt((input[i] & 0xF0) >> 4);
output += hex.charAt(input[i] & 0x0F);
}
return output;
}
//I should replace this with: int("0x" + ...)
private function fromBase16(input:String):ByteArray
{
var output:ByteArray = new ByteArray;
const hex:Object = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'a':10,'b':11,'c':12,'d':13,'e':14,'f':15,'A':10,'B':11,'C':12,'D':13,'E':14,'F':15};
const length:int = input.length;
var i:int = 0;
while(i < length) {
var byte:uint = (hex[input.charAt(i)] << 4);
i++;
if(i < length) {
byte += hex[input.charAt(i)];
i++;
}
output.writeByte(byte);
}
return output;
}
private function bArrayToBase64(input:ByteArray):String {
var base64Enc:Base64Encoder = new Base64Encoder;
base64Enc.encodeBytes(input);
return (base64Enc.toString());
}
private function stringToBase64(input:String):String {
try {
var base64Enc:Base64Encoder = new Base64Encoder;
base64Enc.encode(input);
return (base64Enc.toString());
} catch (e:Error) {
//Bug. This alert box never actually shows.
Alert.show("Error: " + e.message, "ERROR",4,this);
}
return ("ERROR: Could not encode value.");
}
private function fromBase64String(input:String):ByteArray {
var base64Dec:Base64Decoder = new Base64Decoder;
try {
base64Dec.decode(input);
return (base64Dec.toByteArray());
} catch (e:Error) {
//Although it isn't documented, the above functions can throw errors.
//Bug. The Alert box never actually shows.
Alert.show("ERROR: " + e.message,"Error",4,this);
}
return(null);
}
/**
private function initKeyAndIV():void
{
as3_crypto_wrapper.prng_seed(null);
if(null == aes128key)
aes128key = as3_crypto_wrapper.prng_random(16);
if(null == aes128iv)
aes128iv = as3_crypto_wrapper.prng_random(16);
}
*/
private function onSHA1():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.sha1_begin();
as3_crypto_wrapper.sha1_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.sha1_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
private function onSHA256():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.sha256_begin();
as3_crypto_wrapper.sha256_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.sha256_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
private function onMD5():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
var hdl:uint = as3_crypto_wrapper.md5_begin();
as3_crypto_wrapper.md5_update(hdl, buffer);
var digest:ByteArray = as3_crypto_wrapper.md5_finish(hdl);
this.stringOut.text = "";
this.hexOut.text = toBase16(digest);
this.base64out.text = bArrayToBase64(digest);
}
/**
private function onEncryptAES():void
{
var buffer:ByteArray = new ByteArray;
buffer.writeUTF(dataText.text);
initKeyAndIV();
var hdl:uint = as3_crypto_wrapper.aes128_cbc_encrypt_begin(aes128key, aes128iv);
var encrypted:ByteArray = as3_crypto_wrapper.symmetric_encrypt_update(hdl, buffer);
encrypted.writeBytes(as3_crypto_wrapper.symmetric_encrypt_finish(hdl));
//Not sure why they write out the buffer? Make sure it is clear?
this.stringOut.text = toBase16(buffer);
encrypted.position = 0;
this.hexOut.text = toBase16(encrypted);
this.base64out.text = bArrayToBase64(encrypted);
}
private function onDecryptAES():void
{
var buffer:ByteArray = fromBase16(this.hexOut.text);
var hdl:uint = as3_crypto_wrapper.aes128_cbc_decrypt_begin(aes128key, aes128iv);
var decrypted:ByteArray = as3_crypto_wrapper.symmetric_decrypt_update(hdl, buffer);
decrypted.writeBytes(as3_crypto_wrapper.symmetric_decrypt_finish(hdl));
this.hexOut.text = toBase16(decrypted);
decrypted.position = 0;
this.stringOut.text = decrypted.readUTF();
}
private function onGetRandomData():void
{
as3_crypto_wrapper.prng_seed(null);
var buffer:ByteArray = as3_crypto_wrapper.prng_random(20);
dataText.text = toBase16(buffer);
}
*/
private function onBase64Decode():void {
var bArray:ByteArray = fromBase64String(this.dataText.text);
if (bArray == null) {
this.stringOut.text = "ERROR! Could not decode Base64 data.";
this.hexOut.text = "";
this.base64out.text = "";
return;
}
this.stringOut.text = bArray.toString();
this.hexOut.text = this.toBase16(bArray);
this.base64out.text = "";
}
private function onBase64Encode():void {
var output:String = stringToBase64(this.dataText.text);
this.stringOut.text = "";
this.hexOut.text = "";
this.base64out.text = output;
}
private function onHexEncode():void {
var bArray:ByteArray = new ByteArray;
bArray.writeUTFBytes(this.dataText.text);
this.hexOut.text = toBase16(bArray);
this.stringOut.text = "";
this.base64out.text = "";
}
private function onHexDecode():void {
var bArray:ByteArray;
bArray = fromBase16(this.dataText.text);
var tmpString:String = "";
for (var i:int = 0; i < bArray.length; i++) {
if (bArray[i] > 31 && bArray[i] < 128) {
tmpString += String.fromCharCode(bArray[i].toString());
} else {
tmpString += "*";
}
}
this.stringOut.text = tmpString;
this.hexOut.text = "";
this.base64out.text = "";
}
private function onUrlEnc():void {
var regEx:RegExp = new RegExp("[a-zA-Z0-9\,\-\_\.\!\~\*\'\(\)]");
var pos:int = 0;
var orig:String = this.dataText.text;
var encStr:String = "";
while (pos < orig.length) {
if (!(regEx.test(orig.charAt(pos)))) {
var bArray:ByteArray = new ByteArray();
bArray[0] = int(orig.charCodeAt(pos));
encStr += "%" + toBase16(bArray).toUpperCase();
} else {
encStr += orig.charAt(pos);
}
pos++;
}
this.stringOut.text = encStr;
this.base64out.text = "";
this.hexOut.text = ""
}
private function onUrlDec():void {
var regEx:RegExp = new RegExp("\%[a-fA-F0-9]{2}");
var pos:int = 0;
var orig:String = this.dataText.text;
var decStr:String = "";
while (pos < orig.length) {
if (orig.charAt(pos) == "%" && regEx.test(orig.substr(pos,3))) {
var bArray:ByteArray = fromBase16(orig.charAt(pos+1) + orig.charAt(pos+2));
if (bArray[0] > 31 && bArray[0] < 128) {
decStr += String.fromCharCode(bArray[0].toString());
} else {
decStr += "*";
}
pos += 3;
} else {
decStr += orig.charAt(pos);
pos++;
}
}
this.stringOut.text = decStr;
this.base64out.text = "";
this.hexOut.text = ""
}
private function callEscape():void {
var decStr:String = escape(dataText.text);
this.stringOut.text = decStr;
//this.base64out.text = stringToBase64(decStr);
//var ba:ByteArray = new ByteArray();
//ba.writeUTFBytes(decStr);
this.base64out.text = "";
this.hexOut.text = "";
}
private function callUnescape():void {
var decStr:String = unescape(dataText.text);
this.stringOut.text = decStr;
this.base64out.text = stringToBase64(decStr);
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(decStr);
this.hexOut.text = toBase16(ba);
}
private function setFocusManager():void {
if (this.focusManager == null) {
this.fm = new FocusManager(this);
this.dataText.focusManager = this.fm;
this.stringOut.focusManager = this.fm;
this.base64out.focusManager = this.fm;
this.hexOut.focusManager = this.fm;
}
}
]]>
</fx:Script>
<s:Group id="decCanvas" width="529" height="467" initialize="setFocusManager()">
<s:Label x="10" y="10" text="Input"/>
<s:TextArea id="dataText" right="10" left="10" top="26" height="51"/>
<s:Button x="10" y="85" label="Digest SHA1" id="sha1" click="onSHA1()"/>
<s:Button x="10" y="115" label="Digest SHA256" id="sha256" click="onSHA256()"/>
<s:Button x="10" y="145" label="Digest MD5" id="md5" click="onMD5()"/>
<s:Button x="126" y="85" label="Base64 Encode" click="onBase64Encode()"/>
<s:Button x="126" y="115" label="Base64 Decode" click="onBase64Decode()"/>
<s:Button x="247" y="85" label="Hex Encode" click="onHexEncode()"/>
<s:Button x="246" y="115" label="Hex Decode" click="onHexDecode()" width="94"/>
<s:Button x="347" y="85" label="encodeURI" click="onUrlEnc()"/>
<s:Button x="348" y="115" label="decodeURI" click="onUrlDec()"/>
<s:Button x="435" y="85" label="escape" click="callEscape()"/>
<s:Button x="436" y="114" label="unescape" click="callUnescape()"/>
<s:Label x="10" y="186" text="String"/>
<s:TextArea id="stringOut" x="10" y="202" width="509" height="63"/>
<s:Label x="10" y="285" text="Hex16" width="43"/>
<s:TextArea id="hexOut" x="10" y="299" width="509" height="62"/>
<s:Label x="10" y="381" text="Base64"/>
<s:TextArea id="base64out" x="10" y="395" width="509" height="63"/>
</s:Group>
</s:Application>