Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Last active December 22, 2015 08:29
Show Gist options
  • Select an option

  • Save ThomasBurleson/6445197 to your computer and use it in GitHub Desktop.

Select an option

Save ThomasBurleson/6445197 to your computer and use it in GitHub Desktop.
AngularJS using module pattern and AMD pattern (e.g RequireJS) - Shows how the define() is encapsulated without global variable requirements - Shows how `supplant` functionality is dynamically loaded and injected as a define() MD5Factory argument. - Shows how RequireJS registers the MD5 constructor now accessible to all RequireJS subscribers usi…
/**
* Cryptographic library - md5 cipher
*
* @author Thomas Burleson
*
*/
(function( define ) {
"use strict";
var dependencies = [
'utils/supplant'
];
define( dependencies, function md5_Registration( supplant )
{
function rhex(n) {
var s = '', j = 0;
for (; j < 4; j++) {
s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
}
return s;
}
function hex(x)
{
for (var i = 0; i < x.length; i++) {
x[i] = rhex(x[i]);
}
return x.join('');
}
// Publish ONLY the md5 constructor function
return function md5() {
// Build an instance of the md5 object with encrypt() features only...
return {
encrypt : function(s)
{
var results = hex(s);
console.log( supplant( 'md5::encrypt({0}) = {1}',[ s, results] ));
return results;
}
};
};
});
}( define ));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment