gwjjeff / cryptojs

Following googlecode project crypto-js, provide standard and secure cryptographic algorithms for NodeJS. Support MD5, SHA-1, SHA-256, RC4, Rabbit, AES, DES, PBKDF2, HMAC, OFB, CFB, CTR, CBC, Base64
326 stars 74 forks source link

Load with requirejs #6

Open jcalonso opened 11 years ago

jcalonso commented 11 years ago

Is possible to use load this lib with requirejs in frontend?

njoyard commented 11 years ago

It is, but you have to wrap the root cryptojs file as described here

jcalonso commented 11 years ago

Thanks for that, I ended up doing something like this: This is for AES module.

require.config({
    modules: [
        {
            name: 'main'
        }
    ],
    paths: {
        'crypto': "../components/cryptojs/lib/Crypto",
        'crypto.BlockModes': "../components/cryptojs/lib/BlockModes",
        'crypto.PBKDF2': "../components/cryptojs/lib/PBKDF2",
        'crypto.HMAC': "../components/cryptojs/lib/HMAC",
        'crypto.SHA1': "../components/cryptojs/lib/SHA1",
        'crypto.AES': "../components/cryptojs/lib/AES"
    },
    shim: {
        'crypto.AES': ['crypto','crypto.BlockModes','crypto.PBKDF2','crypto.HMAC','crypto.SHA1']
    }

});

require([
    'crypto.AES'
], function () {

    Crypto.AES.encrypt('password', 'passphrase');
});
xenoterracide commented 10 years ago

are you sure there aren't changes that could be made to this that could make it simply work by using packages support in requirejs http://requirejs.org/docs/api.html#packages ? without breaking commonjs support?

xenoterracide commented 10 years ago
paths: {
    'crypto'               :'/bower_components/cryptojs/lib/Crypto',
    'crypto.MD5'      :'/bower_components/cryptojs/lib/MD5',
},
shim: {
    'crypto.MD5'      :['crypto'],
},
// ... in my controller module (note:angular bits are simply to give such users an idea
define(['crypto.MD5', 'angular'], function() { return [ '$scope',   function ( $scope ) {
         var digest = Crypto.MD5( $scope.code ).toString();
     }];
 });

I think it's probably important not to try to use Crypto in the "requirejs name" because it will override the global name export or somethin'. Also usage is based on that global name not the require name.