@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 +3 -3
- package/crypto.sh +43 -0
- package/decrypt.sh +17 -4
- package/encrypt.sh +17 -4
- package/lib/index.cjs +1 -1
- package/package.json +1 -1
- package/src/index.mjs +7 -7
package/crypto.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
declare module '@sequencemedia/crypto' {
|
|
2
|
-
export function hashKey(
|
|
3
|
-
export function
|
|
4
|
-
export function
|
|
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
|
-
|
|
4
|
-
node ./src/shell/decrypt.mjs < "$1"
|
|
5
|
-
}
|
|
3
|
+
source ./crypto.sh
|
|
6
4
|
|
|
7
|
-
|
|
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
|
-
|
|
4
|
-
node ./src/shell/encrypt.mjs < "$1"
|
|
5
|
-
}
|
|
3
|
+
source ./crypto.sh
|
|
6
4
|
|
|
7
|
-
|
|
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(
|
|
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
package/src/index.mjs
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import crypto from 'crypto'
|
|
2
2
|
|
|
3
|
-
export function hashKey (
|
|
4
|
-
if (!
|
|
3
|
+
export function hashKey (secret) {
|
|
4
|
+
if (!secret) throw new Error('A secret is required')
|
|
5
5
|
|
|
6
|
-
return crypto.createHash('sha256').update(
|
|
6
|
+
return crypto.createHash('sha256').update(secret).digest('base64').substring(0, 32)
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export function encrypt (buffer,
|
|
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(
|
|
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,
|
|
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(
|
|
17
|
+
const decipher = crypto.createDecipheriv(algorithm, hashKey(secret), iv)
|
|
18
18
|
return Buffer.concat([decipher.update(buffer.slice(bytes)), decipher.final()])
|
|
19
19
|
}
|