rjutils-collection
Advanced tools
Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "rjutils-collection", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Easy and Lightweight Utilities", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -120,3 +120,20 @@ const path = require('path') | ||
return data | ||
} | ||
}, | ||
/** | ||
* Hash a String | ||
* | ||
* @typedef {Object} decryptStr { algorithm: string, key: string, key: string } | ||
* @prop {String} [algorithm] The Algorithm to use | ||
* @prop {String} [digest] The Text Output (base64, base64url, binary, hex) | ||
* @prop {String} text The Text to Hash | ||
* | ||
* @param {decryptStr} options | ||
*/ | ||
hashString(options) { | ||
if (typeof options !== 'object') throw new TypeError('options must be an object') | ||
const data = require('./utils/cryptString').hash(options) | ||
return data | ||
}, | ||
} |
const crypto = require('crypto') | ||
module.exports.encrypt = (json) => { | ||
const key = crypto.createHash('sha256').update(String(json.key)).digest('base64').substring(0, 32) | ||
const algorithm = json.algorithm || 'aes-256-cbc' | ||
const output = json.output || 'hex' | ||
module.exports.encrypt = (options) => { | ||
const key = crypto.createHash('sha256').update(String(options.key)).digest('base64').substring(0, 32) | ||
const algorithm = options.algorithm || 'aes-256-cbc' | ||
const output = options.output || 'hex' | ||
const iv = Buffer.alloc(16, 0) | ||
const enCipher = crypto.createCipheriv(algorithm, key, iv) | ||
let encryptedData = enCipher.update(json.text) | ||
let encryptedData = enCipher.update(options.text) | ||
encryptedData = Buffer.concat([encryptedData, enCipher.final()]) | ||
@@ -16,6 +16,6 @@ | ||
module.exports.decrypt = (json) => { | ||
const key = crypto.createHash('sha256').update(String(json.key)).digest('base64').substring(0, 32) | ||
const algorithm = json.algorithm || 'aes-256-cbc' | ||
const output = json.output || 'utf8' | ||
module.exports.decrypt = (options) => { | ||
const key = crypto.createHash('sha256').update(String(options.key)).digest('base64').substring(0, 32) | ||
const algorithm = options.algorithm || 'aes-256-cbc' | ||
const output = options.output || 'utf8' | ||
const iv = Buffer.alloc(16, 0) | ||
@@ -25,6 +25,15 @@ | ||
let decryptedData = deCipher.update(Buffer.from(json.text, 'hex')) | ||
let decryptedData = deCipher.update(Buffer.from(options.text, 'hex')) | ||
decryptedData = Buffer.concat([decryptedData, deCipher.final()]) | ||
return decryptedData.toString(output) | ||
} | ||
module.exports.hash = (options) => { | ||
const algorithm = options.algorithm || 'sha512' | ||
const digest = options.digest || 'hex' | ||
const hash = crypto.createHash(algorithm).update(String(options.text)).digest(digest).substring(0, 32) | ||
return hash | ||
} |
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
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
10698
202