100x times faster MD5 and more

Update: Get more recent version of the library here.

Update: I updated the zip with LGPL MIT license file. The original author basically says “use wherever you want” which looks like mostly LGPL MIT, right? Yes, haXe was used to create the SWC but I don’t know if we can get the source or not. Anyway I hope you understand from the code below how to use the library.

My dear friend BlooDHounD has recently released an SWC with highly optimized MD5, Base64, CRC32, JPEG, PNG algorithms. Compared to as3corelib we get the following results:

by.blooddy.crypto.MD5.hashBytes: 40
com.adobe.crypto.MD5.hashBytes: 4483

by.blooddy.crypto.Base64.encode: 115
mx.utils.Base64Encoder: 1635

by.blooddy.crypto.Base64.decode: 141
mx.utils.Base64Decoder: 2762

by.blooddy.crypto.image.JPEGEncoder.encode: 447
com.adobe.images.JPGEncoder: 3496

by.blooddy.crypto.image.PNG24Encoder.encode: 538
com.adobe.images.PNGEncoder.encode: 1423

Here’s the benchmark source:

package {

	import by.blooddy.crypto.Base64;
	import by.blooddy.crypto.MD5;
	import by.blooddy.crypto.image.JPEGEncoder;
	import by.blooddy.crypto.image.PNG24Encoder;

	import com.adobe.crypto.MD5;
	import com.adobe.images.JPGEncoder;
	import com.adobe.images.PNGEncoder;

	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;

	import mx.utils.Base64Decoder;
	import mx.utils.Base64Encoder;

	[SWF( scriptTimeLimit="255" )]
	public class test extends Sprite {

		/**
		 * Constructor
		 */
		public function test() {
			super();

			var text:String = '';
			var t:Number;

			// BitmapData for tests
			var bmp:BitmapData = new BitmapData( 1024, 1024, true, 0xFFFF0000 );
			bmp.noise( int( Math.random() * int.MAX_VALUE ), 0, 0xFF, 7, false ); // генерируем шум

			// ByteArray for tests
			var bytes:ByteArray = bmp.getPixels( bmp.rect );

			// MD5 test
			t = getTimer();
			by.blooddy.crypto.MD5.hashBytes( bytes );
			text += '\nby.blooddy.crypto.MD5.hashBytes: ' + ( getTimer() - t ); // 40

			t = getTimer();
			com.adobe.crypto.MD5.hashBytes( bytes );
			text += '\ncom.adobe.crypto.MD5.hashBytes: ' + ( getTimer() - t ); // 4483

			// Base64 encode test
			t = getTimer();
			var s1:String = by.blooddy.crypto.Base64.encode( bytes, true );
			text += '\nby.blooddy.crypto.Base64.encode: ' + ( getTimer() - t ); // 115

			t = getTimer();
			var e:Base64Encoder = new Base64Encoder();
			e.encodeBytes( bytes );
			var s2:String = e.flush();
			text += '\nmx.utils.Base64Encoder: ' + ( getTimer() - t ); // 1635

			// Base64 decode test
			t = getTimer();
			by.blooddy.crypto.Base64.decode( s1 );
			text += '\nby.blooddy.crypto.Base64.decode: ' + ( getTimer() - t ); // 141

			t = getTimer();
			var d:Base64Decoder = new Base64Decoder();
			d.decode( s2 );
			d.flush();
			text += '\nmx.utils.Base64Decoder: ' + ( getTimer() - t ); // 2762

			// JPEG test
			t = getTimer();
			by.blooddy.crypto.image.JPEGEncoder.encode( bmp, 60 );
			text += '\nby.blooddy.crypto.image.JPEGEncoder.encode: ' + ( getTimer() - t ); // 447

			t = getTimer();
			( new com.adobe.images.JPGEncoder( 60 ) ).encode( bmp );
			text += '\ncom.adobe.images.JPGEncoder: ' + ( getTimer() - t ); // 3496

			// PNG test
			t = getTimer();
			by.blooddy.crypto.image.PNG24Encoder.encode( bmp );
			text += '\nby.blooddy.crypto.image.PNG24Encoder.encode: ' + ( getTimer() - t ); // 538

			t = getTimer();
			com.adobe.images.PNGEncoder.encode( bmp );
			text += '\ncom.adobe.images.PNGEncoder.encode: ' + ( getTimer() - t ); // 1423

			var tf:TextField = new TextField();
			tf.autoSize = 'left';
			tf.text = text;
			super.addChild( tf );

		}

	}

}

I know that he is too shy to tell you guys about his results (8
But they are definitely worth sharing.

You can download the SWC here (look up at the update section).

13 Responses to “100x times faster MD5 and more”


Leave a Reply