@sequencemedia/crypto 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ module.exports = {
2
+ compact: true,
3
+ comments: false,
4
+ presets: [
5
+ [
6
+ '@babel/env', {
7
+ targets: {
8
+ node: 'current'
9
+ },
10
+ useBuiltIns: 'usage',
11
+ corejs: 3
12
+ }
13
+ ]
14
+ ]
15
+ }
package/lib/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.decrypt=decrypt;exports.encrypt=encrypt;exports.hashKey=hashKey;var _crypto=_interopRequireDefault(require("crypto"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function hashKey(key){if(!key)throw new Error('A key is required');return _crypto.default.createHash('sha256').update(key).digest('base64').substr(0,32);}function encrypt(buffer,key,algorithm='aes-256-ctr'){const iv=_crypto.default.randomBytes(16);const cipher=_crypto.default.createCipheriv(algorithm,hashKey(key),iv);return Buffer.concat([iv,cipher.update(buffer),cipher.final()]);}function decrypt(buffer,key,algorithm='aes-256-ctr'){const iv=buffer.slice(0,16);const decipher=_crypto.default.createDecipheriv(algorithm,hashKey(key),iv);return Buffer.concat([decipher.update(buffer.slice(16)),decipher.final()]);}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@sequencemedia/crypto",
3
+ "version": "1.0.0",
4
+ "main": "./lib/index.cjs",
5
+ "type": "module",
6
+ "author": {
7
+ "name": "Jonathan Perry for Sequence Media Limited",
8
+ "email": "sequencemedia@sequencemedia.net",
9
+ "url": "http://sequencemedia.net"
10
+ },
11
+ "license": "ISC",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git@github.com:sequencemedia/crypto.git"
15
+ },
16
+ "scripts": {
17
+ "build": "npm run babel",
18
+ "watch": "npm run babel -- -w",
19
+ "clean": "rimraf lib",
20
+ "babel": "babel src -d lib --out-file-extension .cjs",
21
+ "prepublishOnly": "npm run build",
22
+ "prebuild": "npm run clean",
23
+ "lint": "eslint . --ext .mjs --ext .cjs",
24
+ "lint:fix": "npm run lint -- --fix",
25
+ "pre-commit": "npm run lint && npm run build && git add lib",
26
+ "prepare": "husky install"
27
+ },
28
+ "devDependencies": {
29
+ "@babel/cli": "^7.19.3",
30
+ "@babel/core": "^7.20.2",
31
+ "@babel/eslint-parser": "^7.19.1",
32
+ "@babel/preset-env": "^7.20.2",
33
+ "core-js": "^3.26.1",
34
+ "eslint": "^8.28.0",
35
+ "eslint-config-standard": "^17.0.0",
36
+ "eslint-plugin-import": "^2.26.0",
37
+ "eslint-plugin-node": "^11.1.0",
38
+ "eslint-plugin-promise": "^6.1.1",
39
+ "husky": "^8.0.2"
40
+ },
41
+ "exports": {
42
+ "import": "./src/index.mjs",
43
+ "require": "./lib/index.cjs"
44
+ }
45
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ import crypto from 'crypto'
2
+
3
+ export function hashKey (key) {
4
+ if (!key) throw new Error('A key is required')
5
+
6
+ return crypto.createHash('sha256').update(key).digest('base64').substr(0, 32)
7
+ }
8
+
9
+ export function encrypt (buffer, key, algorithm = 'aes-256-ctr') {
10
+ const iv = crypto.randomBytes(16)
11
+ const cipher = crypto.createCipheriv(algorithm, hashKey(key), iv)
12
+ return Buffer.concat([iv, cipher.update(buffer), cipher.final()])
13
+ }
14
+
15
+ export function decrypt (buffer, key, algorithm = 'aes-256-ctr') {
16
+ const iv = buffer.slice(0, 16)
17
+ const decipher = crypto.createDecipheriv(algorithm, hashKey(key), iv)
18
+ return Buffer.concat([decipher.update(buffer.slice(16)), decipher.final()])
19
+ }