cryptojs class to script include

ShAn21
Tera Guru

Hi Team,

How can we copy the full contents of cryptoJS from CDN to script include and start using it server side?

https://cdnjs.com/libraries/crypto-js

 

Is there any other source we can copy as well I am ok with it. Please let me know if you have any feedback

 

Thanks

1 ACCEPTED SOLUTION

Hi @jMarshal 

The below code works as is in script include. The only catch is it works only in custom scope.

https://gist.github.com/vetsin/117cfe99ab0f87c1ac2d5c6387d1f7b4

 

Thanks

View solution in original post

5 REPLIES 5

rvaletov1
Tera Contributor

Figured it out and it works

 

  1. Download latest CryptoJS package http://code.google.com/p/crypto-js/downloads/list
  2. From the zipped 'components' folder, extract core.js file and .js files for desired algorithm. example sha256.js
  3. In ServiceNow instance create a new scoped application
  4. Within that scope application, create a new Script Include 
  5. In the Script Include script, add code below and ENSURE to copy and paste contents from the extracted .js files (can use Notepad to open them)
/** copy and paste contents of core.js
*  should start with 'var CryptoJS = CryptoJS ||'  etc.
* /

/** copy and paste contents of the chosen algorith js file 
* example of sha256.js
* should start with '(function (Math) {' etc. 
* /

//Script include definition here with SHA256 example
var MyCryptoScript = Class.create();
MyCryptoScript.prototype = {
    initialize: function() {
    },

    hashSHA256: function(input) {
        return CryptoJS.SHA256(input).toString(CryptoJS.enc.Hex);
    },

 type: 'MyCryptoScript'
};
​
  • Test calling the Script Include 
var crypto = new MyCryptoScript();
var hash = crypto.hashSHA256('your input string');
gs.info('SHA-256 Hash: ' + hash);