@sequencemedia/crypto 1.1.2 → 1.1.3

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.
package/crypto.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  declare module '@sequencemedia/crypto' {
2
- export function hashKey(key: string): string
3
- export function encode(buffer: Buffer, key: string, secret?: string): Buffer
4
- export function decode(buffer: Buffer, key: string, secret?: string): Buffer
2
+ export function hashKey(secret: string): string
3
+ export function encrypt(buffer: Buffer, secret: string, bytes?: number, algorithm?: string): Buffer
4
+ export function decrypt(buffer: Buffer, secret: string, bytes?: number, algorithm?: string): Buffer
5
5
  }
package/crypto.sh ADDED
@@ -0,0 +1,43 @@
1
+ #!/bin/bash
2
+
3
+ encrypt () {
4
+ node ./src/shell/encrypt.mjs < "$1"
5
+ }
6
+
7
+ decrypt () {
8
+ node ./src/shell/decrypt.mjs < "$1"
9
+ }
10
+
11
+ get_args () {
12
+ for flag in "$@";
13
+ do
14
+ shift
15
+ case "$flag" in
16
+ '--origin')
17
+ set -- "$@" '-o'
18
+ ;;
19
+ '--destination')
20
+ set -- "$@" '-d'
21
+ ;;
22
+ *)
23
+ set -- "$@" "$flag"
24
+ ;;
25
+ esac
26
+ done
27
+
28
+ while getopts ":o:d:" flag;
29
+ do
30
+ case "${flag}" in
31
+ o)
32
+ export ORIGIN=$OPTARG
33
+ ;;
34
+ d)
35
+ export DESTINATION=$OPTARG
36
+ ;;
37
+ esac
38
+ done
39
+ }
40
+
41
+ export -f encrypt
42
+
43
+ export -f decrypt
package/decrypt.sh CHANGED
@@ -1,7 +1,20 @@
1
1
  #!/bin/bash
2
2
 
3
- decrypt () {
4
- node ./src/shell/decrypt.mjs < "$1"
5
- }
3
+ source ./crypto.sh
6
4
 
7
- export -f decrypt
5
+ get_args "$@";
6
+
7
+ if [[ -z "$ORIGIN" ]];
8
+ then
9
+ echo Origin is required
10
+ exit 1
11
+ fi
12
+
13
+ if [[ -z "$DESTINATION" ]];
14
+ then
15
+ echo Destination is required
16
+ exit 1
17
+ fi
18
+
19
+ decrypt "$ORIGIN" > "$DESTINATION"
20
+ exit 0
package/encrypt.sh CHANGED
@@ -1,7 +1,20 @@
1
1
  #!/bin/bash
2
2
 
3
- encrypt () {
4
- node ./src/shell/encrypt.mjs < "$1"
5
- }
3
+ source ./crypto.sh
6
4
 
7
- export -f encrypt
5
+ get_args "$@";
6
+
7
+ if [[ -z "$ORIGIN" ]];
8
+ then
9
+ echo Origin is required
10
+ exit 1
11
+ fi
12
+
13
+ if [[ -z "$DESTINATION" ]];
14
+ then
15
+ echo Destination is required
16
+ exit 1
17
+ fi
18
+
19
+ encrypt "$ORIGIN" > "$DESTINATION"
20
+ exit 0
package/lib/index.cjs CHANGED
@@ -1 +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').substring(0,32);}function encrypt(buffer,key,bytes=16,algorithm='aes-256-ctr'){const iv=_crypto.default.randomBytes(bytes);const cipher=_crypto.default.createCipheriv(algorithm,hashKey(key),iv);return Buffer.concat([iv,cipher.update(buffer),cipher.final()]);}function decrypt(buffer,key,bytes=16,algorithm='aes-256-ctr'){const iv=buffer.slice(0,bytes);const decipher=_crypto.default.createDecipheriv(algorithm,hashKey(key),iv);return Buffer.concat([decipher.update(buffer.slice(bytes)),decipher.final()]);}
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(secret){if(!secret)throw new Error('A secret is required');return _crypto.default.createHash('sha256').update(secret).digest('base64').substring(0,32);}function encrypt(buffer,secret,bytes=16,algorithm='aes-256-ctr'){const iv=_crypto.default.randomBytes(bytes);const cipher=_crypto.default.createCipheriv(algorithm,hashKey(secret),iv);return Buffer.concat([iv,cipher.update(buffer),cipher.final()]);}function decrypt(buffer,secret,bytes=16,algorithm='aes-256-ctr'){const iv=buffer.slice(0,bytes);const decipher=_crypto.default.createDecipheriv(algorithm,hashKey(secret),iv);return Buffer.concat([decipher.update(buffer.slice(bytes)),decipher.final()]);}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sequencemedia/crypto",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "main": "./lib/index.cjs",
5
5
  "type": "module",
6
6
  "types": "./crypto.d.ts",
package/src/index.mjs CHANGED
@@ -1,19 +1,19 @@
1
1
  import crypto from 'crypto'
2
2
 
3
- export function hashKey (key) {
4
- if (!key) throw new Error('A key is required')
3
+ export function hashKey (secret) {
4
+ if (!secret) throw new Error('A secret is required')
5
5
 
6
- return crypto.createHash('sha256').update(key).digest('base64').substring(0, 32)
6
+ return crypto.createHash('sha256').update(secret).digest('base64').substring(0, 32)
7
7
  }
8
8
 
9
- export function encrypt (buffer, key, bytes = 16, algorithm = 'aes-256-ctr') {
9
+ export function encrypt (buffer, secret, bytes = 16, algorithm = 'aes-256-ctr') {
10
10
  const iv = crypto.randomBytes(bytes)
11
- const cipher = crypto.createCipheriv(algorithm, hashKey(key), iv)
11
+ const cipher = crypto.createCipheriv(algorithm, hashKey(secret), iv)
12
12
  return Buffer.concat([iv, cipher.update(buffer), cipher.final()])
13
13
  }
14
14
 
15
- export function decrypt (buffer, key, bytes = 16, algorithm = 'aes-256-ctr') {
15
+ export function decrypt (buffer, secret, bytes = 16, algorithm = 'aes-256-ctr') {
16
16
  const iv = buffer.slice(0, bytes)
17
- const decipher = crypto.createDecipheriv(algorithm, hashKey(key), iv)
17
+ const decipher = crypto.createDecipheriv(algorithm, hashKey(secret), iv)
18
18
  return Buffer.concat([decipher.update(buffer.slice(bytes)), decipher.final()])
19
19
  }