@sequencemedia/crypto 1.1.2 → 1.1.4

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/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # @sequencemedia/crypto
2
+
3
+ Encrypt and decrypt in Node and Bash
4
+
5
+ ## Node
6
+
7
+ Exports two functions, `encrypt` and `decrypt`
8
+
9
+ ```javascript
10
+ import {
11
+ encrypt,
12
+ decrypt
13
+ } from '@sequencemedia/crypto'
14
+ ```
15
+
16
+ ### `encrypt`
17
+
18
+ ```typescript
19
+ function encrypt(buffer: Buffer, secret: string, bytes?: number, algorithm?: string): Buffer
20
+ ```
21
+
22
+ Only `buffer` and `secret` are _required_
23
+
24
+ - `buffer` is the data to encrypt
25
+ - `secret` is the secret key to use for encryption
26
+
27
+ Both `bytes` and `algorithm` are _optional_
28
+
29
+ - `bytes` is the number of random bytes to use for the encryption _initialisation vector_. The default is `16`
30
+ - `algorithm` is the algorithm to use for encryption. The default is `aes-256-ctr`
31
+
32
+ ### `decrypt`
33
+
34
+ ```typescript
35
+ function decrypt(buffer: Buffer, secret: string, bytes?: number, algorithm?: string): Buffer
36
+ ```
37
+
38
+ Only `buffer` and `secret` are _required_
39
+
40
+ - `buffer` is the data to decrypt
41
+ - `secret` is the secret key to use for decryption
42
+
43
+ Both `bytes` and `algorithm` are _optional_
44
+
45
+ - `bytes` is the number of bytes to slice from the buffer for the decryption _initialisation vector_. The default is `16`
46
+ - `algorithm` is the algorithm to use for decryption. The default is `aes-256-ctr`
47
+
48
+ ## Bash
49
+
50
+ Exports two functions, `encrypt` and `decrypt`
51
+
52
+ ```bash
53
+ source ./crypto.sh
54
+ ```
55
+
56
+ ### `encrypt`
57
+
58
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a file path to _encrypt_
59
+
60
+ ```bash
61
+ encrypted_file_data=$(encrypt "./file.txt")
62
+ ```
63
+
64
+ ```bash
65
+ encrypt "./file.txt" > "./encrypted.txt"
66
+ ```
67
+
68
+ ### `decrypt`
69
+
70
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a file path to _decrypt_
71
+
72
+ ```bash
73
+ file_data=$(decrypt "./encrypted.txt")
74
+ ```
75
+
76
+ ```bash
77
+ decrypt "./encrypted.txt" > "./file.txt"
78
+ ```
79
+
80
+ ## Bash utilities
81
+
82
+ ### `encrypt.sh`
83
+
84
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment
85
+
86
+ - A file _origin_ path for data to _encrypt_
87
+ - A file _destination_ path to put the encrypted data
88
+
89
+ ```bash
90
+ CRYPTO_KEY='secret' ./encrypt.sh --origin "./file.txt" --destination "./encrypted.txt"
91
+ ```
92
+
93
+ ### `decrypt.sh`
94
+
95
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment
96
+
97
+ - A file _origin_ path for data to _decrypt_
98
+ - A file _destination_ path to put the decrypted data
99
+
100
+ ```bash
101
+ CRYPTO_KEY='secret' ./decrypt.sh --origin "./encrypted.txt" --destination "./file.txt"
102
+ ```
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.4",
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
  }