adyen-encrypt
Advanced tools
Comparing version 0.1.6 to 0.1.7
"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();var _sjcl=require("sjcl");var _sjcl2=_interopRequireDefault(_sjcl);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{"default":obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}var AES=function(){function AES(){_classCallCheck(this,AES);_sjcl2.default.random.startCollectors();this._key=_sjcl2.default.random.randomWords(8,0)}_createClass(AES,[{key:"encrypt",value:function encrypt(text){var aes=new _sjcl2.default.cipher.aes(this._key);var bits=_sjcl2.default.codec.utf8String.toBits(text);var iv=_sjcl2.default.random.randomWords(3,0);var cipher=_sjcl2.default.mode.ccm.encrypt(aes,bits,iv);var cipherIv=_sjcl2.default.bitArray.concat(iv,cipher);return _sjcl2.default.codec.base64.fromBits(cipherIv)}}]);return AES}();exports.default=AES;"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();var _sjcl=require("sjcl");var _sjcl2=_interopRequireDefault(_sjcl);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{"default":obj}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}var AdyenEncrypt=function(){function AdyenEncrypt(key){var _this=this;var opts=arguments.length<=1||arguments[1]===undefined?{}:arguments[1];_classCallCheck(this,AdyenEncrypt);if(!key){throw new Error("No key provided.")}this._key=key;this._opts=opts;this._encryptVersion="0_1_15";_sjcl2.default.random.startCollectors();if(typeof this._opts.numberIgnoreNonNumeric==="undefined"){this._opts.numberIgnoreNonNumeric=true}if(typeof this._opts.cvcIgnoreFornumber!=="undefined"){delete this._opts.cvcIgnoreFornumber}if(typeof this._opts.cvcIgnoreBins==="string"){(function(){var binsToIgnore=[];_this._opts.cvcIgnoreBins.replace(/\d+/g,function(m){if(m.length>0&&!isNaN(parseInt(m,10))){binsToIgnore.push(m)}return m});if(binsToIgnore.length>0){_this._opts.cvcIgnoreFornumber=new RegExp("^\\s*("+binsToIgnore.join("|")+")")}})()}else if(typeof this._opts.cvcIgnoreBins!=="undefined"){delete this._opts.cvcIgnoreBins}}_createClass(AdyenEncrypt,[{key:"encrypt",value:function encrypt(data){var validations={number:data.number||"",cvc:data.cvc||"",month:data.expiryMonth||"",year:data.expiryYear||"",holderName:data.holderName||""};if(this._opts.enableValidations){var errors=this.validate(validations);if(errors.length){return{valid:false,errors:errors}}}var rsa=this.createRSAKey();var aes=new AES;var cypher=aes.encrypt(JSON.stringify(data));var bytes=_sjcl2.default.codec.bytes.fromBits(aes._key);var encrypted=rsa.encrypt_b64(bytes);var prefix="adyenjs_"+this._encryptVersion+"$";return{valid:true,value:""+prefix+encrypted+"$"+cypher}}},{key:"createRSAKey",value:function createRSAKey(){if(!this._key){throw new Error("Missing key.")}var key=this._key.split("|");if(key.length!==2){throw new Error("Invalid public key.")}var mod=key[1];var exp=key[0];var rsa=new RSAKey;rsa.setPublic(mod,exp);return rsa}},{key:"validate",value:function validate(data){var _this2=this;if((typeof data==="undefined"?"undefined":_typeof(data))!=="object"){return false}var out=[];Object.keys(data).forEach(function(field){var val=data[field];var ignore=false;if(_this2._opts[field+"IgnoreNonNumeric"]){val=val.replace(/D/g,"")}Object.keys(data).forEach(function(relatedField){var possibleOption=_this2._opts[field+"IgnoreFor"+relatedField];if(possibleOption&&data[relatedField].match(possibleOption)){ignore=true}});if(ignore){return}var fieldCheck={number:_this2._checkNumber.bind(_this2),cvc:_this2._checkCvc.bind(_this2),expiryYear:_this2._checkYear.bind(_this2),year:_this2._checkYear.bind(_this2),expiryMonth:_this2._checkMonth.bind(_this2),month:_this2._checkMonth.bind(_this2),holderName:_this2._checkName.bind(_this2)};if(fieldCheck[field]&&!fieldCheck[field](val)){out.push(field)}});return out}},{key:"_checkLuhn",value:function _checkLuhn(val){if(isNaN(parseInt(val,10))){return false}var length=val.length;var oddEven=length&1;var sum=0;var cache={};if(typeof cache[val]==="undefined"){for(var cpt=0;cpt<length;++cpt){var digit=parseInt(val.charAt(cpt),10);if(!(cpt&1^oddEven)){digit*=2;if(digit>9){digit-=9}}sum+=digit}cache[val]=sum%10===0}return cache[val]}},{key:"_checkNumber",value:function _checkNumber(){var val=arguments.length<=0||arguments[0]===undefined?"":arguments[0];var card=val.replace(/[^\d]/g,"");return card.match(/^\d{10,20}$/)&&this._checkLuhn(card)}},{key:"_checkCvc",value:function _checkCvc(val){return val&&val.match&&val.match(/^\d{3,4}$/)}},{key:"_checkYear",value:function _checkYear(val){return val&&val.match&&val.match(/^\d{4}$/)}},{key:"_checkMonth",value:function _checkMonth(){var val=arguments.length<=0||arguments[0]===undefined?"":arguments[0];var month=val.replace(/^0(\d)$/,"$1");var monthNum=parseInt(month,10);return month.match(/^([1-9]|10|11|12)$/)&&monthNum>=1&&monthNum<=12}},{key:"_checkName",value:function _checkName(val){return val&&val.match&&val.match(/\S/)}}]);return AdyenEncrypt}();exports.default=AdyenEncrypt; | ||
const a = new AdyenEncrypt('efef') | ||
console.log(a) |
{ | ||
"name": "adyen-encrypt", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "An ES2015 Adyen-CSE rewrite", | ||
@@ -5,0 +5,0 @@ "main": "dist", |
@@ -7,3 +7,3 @@ # adyen-encrypt | ||
Please note that it this package will only work with an npm >= 3. | ||
Please note that it this package will only work with an npm >= 3. You also need Java (:sob:) to recompile SJCL. | ||
@@ -10,0 +10,0 @@ ###### Constructor |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
27242
275