@sequencemedia/crypto 1.1.16 → 1.1.18

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 CHANGED
@@ -4,7 +4,7 @@ Encrypt and decrypt in Node and Bash
4
4
 
5
5
  ## Node
6
6
 
7
- Exports two functions, `encrypt` and `decrypt`
7
+ The package _main_ exports two functions, `encrypt` and `decrypt`
8
8
 
9
9
  ```javascript
10
10
  import {
@@ -57,13 +57,28 @@ Both `bytes` and `algorithm` are _optional_
57
57
 
58
58
  ## Bash
59
59
 
60
- Exports two functions, `encrypt` and `decrypt`
60
+ The package contains three scripts
61
+
62
+ 1. `crypto.sh`
63
+ 1. `encrypt.sh`
64
+ 2. `decrypt.sh`
65
+
66
+ Script `crypto.sh` exports four functions to consume in your own Bash scripts
61
67
 
62
68
  ```bash
63
69
  source ./crypto.sh
64
70
  ```
65
71
 
66
- ### `encrypt`
72
+ 1. `encrypt`
73
+ 2. `decrypt`
74
+ 3. `encrypt_directory`
75
+ 4. `decrypt_directory`
76
+
77
+ Scripts [`encrypt.sh`](#encryptsh) and [`decrypt.sh`](#decryptsh) can be executed at the command line
78
+
79
+ ### Bash functions
80
+
81
+ #### `encrypt`
67
82
 
68
83
  Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a file path to _encrypt_
69
84
 
@@ -77,7 +92,7 @@ CRYPTO_KEY='secret'
77
92
  encrypt "./file.txt" > "./encrypted.txt"
78
93
  ```
79
94
 
80
- ### `decrypt`
95
+ #### `decrypt`
81
96
 
82
97
  Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a file path to _decrypt_
83
98
 
@@ -91,32 +106,86 @@ CRYPTO_KEY='secret'
91
106
  decrypt "./encrypted.txt" > "./file.txt"
92
107
  ```
93
108
 
94
- ## Bash scripts
109
+ #### `encrypt_directory`
95
110
 
96
- ### `encrypt.sh`
111
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a directory path to _encrypt_
97
112
 
98
- Requires `CRYPTO_KEY` as a _variable_ in the Bash environment
113
+ - The first argument is the origin directory of files to _encrypt_
114
+ - The second argument is the destination directory for the _encrypted_ files
99
115
 
100
- - A file _origin_ path for data to _encrypt_
101
- - A file _destination_ path to put the encrypted data
116
+ ```bash
117
+ CRYPTO_KEY='secret'
118
+ encrypt_directory "./directory" "./encrypted"
119
+ ```
120
+
121
+ #### `decrypt_directory`
122
+
123
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment and a directory path to _decrypt_
124
+
125
+ - The first argument is the origin directory of files to _decrypt_
126
+ - The second argument is the destination directory for the _decrypted_ files
102
127
 
103
128
  ```bash
104
129
  CRYPTO_KEY='secret'
105
- ./encrypt.sh \
130
+ decrypt_directory "./encrypted" "./directory"
131
+ ```
132
+
133
+ ### Bash scripts
134
+
135
+ #### `encrypt.sh`
136
+
137
+ Requires `CRYPTO_KEY` as a _variable_ in the Bash environment
138
+
139
+ - You can provide either _file_ or _directory_ paths
140
+ - You can provide `--verbose` or `-v`
141
+
142
+ ##### File paths
143
+
144
+ - A file _origin_ path to _encrypt_
145
+ - A file _destination_ path for the _encrypted_ file
146
+
147
+ ```bash
148
+ CRYPTO_KEY='secret' ./encrypt.sh \
106
149
  --origin "./file.txt" \
107
150
  --destination "./encrypted.txt"
108
151
  ```
109
152
 
110
- ### `decrypt.sh`
153
+ ##### Directory paths
154
+
155
+ - A directory _origin_ path to _encrypt_
156
+ - A directory _destination_ path for _encrypted_ files
157
+
158
+ ```bash
159
+ CRYPTO_KEY='secret' ./encrypt.sh \
160
+ --origin "./directory" \
161
+ --destination "./encrypted"
162
+ ```
163
+
164
+ #### `decrypt.sh`
111
165
 
112
166
  Requires `CRYPTO_KEY` as a _variable_ in the Bash environment
113
167
 
114
- - A file _origin_ path for data to _decrypt_
115
- - A file _destination_ path to put the decrypted data
168
+ - You can provide either _file_ or _directory_ paths
169
+ - You can provide `--verbose` or `-v`
170
+
171
+ ##### File paths
172
+
173
+ - A file _origin_ path to _decrypt_
174
+ - A file _destination_ path for the _decrypted_ file
116
175
 
117
176
  ```bash
118
- CRYPTO_KEY='secret'
119
- ./decrypt.sh \
177
+ CRYPTO_KEY='secret' ./decrypt.sh \
120
178
  --origin "./encrypted.txt" \
121
179
  --destination "./file.txt"
122
180
  ```
181
+
182
+ ##### Directory paths
183
+
184
+ - A directory _origin_ path to _decrypt_
185
+ - A directory _destination_ path for _decrypted_ files
186
+
187
+ ```bash
188
+ CRYPTO_KEY='secret' ./decrypt.sh \
189
+ --origin "./encrypted" \
190
+ --destination "./directory"
191
+ ```
package/crypto.sh CHANGED
@@ -1,12 +1,6 @@
1
1
  #!/bin/bash
2
2
 
3
- encrypt () {
4
- node ./src/shell/encrypt.mjs < "$1"
5
- }
6
-
7
- decrypt () {
8
- node ./src/shell/decrypt.mjs < "$1"
9
- }
3
+ VERBOSE=false
10
4
 
11
5
  get_args () {
12
6
  for flag in "$@";
@@ -19,13 +13,16 @@ get_args () {
19
13
  '--destination')
20
14
  set -- "$@" '-d'
21
15
  ;;
16
+ '--verbose')
17
+ set -- "$@" '-v'
18
+ ;;
22
19
  *)
23
20
  set -- "$@" "$flag"
24
21
  ;;
25
22
  esac
26
23
  done
27
24
 
28
- while getopts ":o:d:" flag;
25
+ while getopts ":o:d:v" flag;
29
26
  do
30
27
  case "${flag}" in
31
28
  o)
@@ -34,10 +31,138 @@ get_args () {
34
31
  d)
35
32
  export DESTINATION=$OPTARG
36
33
  ;;
34
+ v)
35
+ VERBOSE=true
36
+ ;;
37
+ *)
38
+ echo Ignoring "$flag"
39
+ ;;
37
40
  esac
38
41
  done
39
42
  }
40
43
 
44
+ get_path () {
45
+ local absolute_path
46
+
47
+ case "${1}" in
48
+ [./]*)
49
+ absolute_path="$(cd "${1%/*}" || exit; pwd)/${1##*/}"
50
+ ;;
51
+ *)
52
+ absolute_path="${!PWD}/${1}"
53
+ ;;
54
+ esac
55
+
56
+ # shellcheck disable=SC2001
57
+ echo "$(sed "s,/$,," <<< "$absolute_path")"
58
+ }
59
+
60
+ encrypt () {
61
+ node ./src/shell/encrypt.mjs < "$1"
62
+ }
63
+
64
+ decrypt () {
65
+ node ./src/shell/decrypt.mjs < "$1"
66
+ }
67
+
68
+ encrypt_directory () {
69
+ local current_directory
70
+ local destination_directory
71
+ local directory_root
72
+
73
+ current_directory=$(get_path "$1")
74
+ destination_directory=$(get_path "$2")
75
+ directory_root=$(get_path "${3-$1}")
76
+
77
+ for child in "$current_directory"/*;
78
+ do
79
+ # Recurse $child directory
80
+ if [ -d "$child" ];
81
+ then
82
+ local file_path
83
+
84
+ [[ "$child" =~ "$directory_root"/(.*)$ ]] && file_path="${BASH_REMATCH[1]}"
85
+
86
+ mkdir -p "$destination_directory/$file_path"
87
+
88
+ local directory="$child"
89
+
90
+ [[ $VERBOSE == true ]] && echo Recursing into "$directory"
91
+
92
+ encrypt_directory "$directory" "$destination_directory" "$directory_root"
93
+ else
94
+ # Ensure $child is a file (not "/*")
95
+ if [ -f "$child" ];
96
+ then
97
+ local file_name
98
+
99
+ [[ "$child" =~ "$directory_root"/(.*) ]] && file_name="${BASH_REMATCH[1]}"
100
+
101
+ mkdir -p "$destination_directory"
102
+
103
+ local file="$child"
104
+
105
+ [[ $VERBOSE == true ]] && echo Encrypting "$file"
106
+
107
+ encrypt "$file" > "$destination_directory/$file_name"
108
+ fi
109
+ fi
110
+ done
111
+ }
112
+
113
+ decrypt_directory () {
114
+ local current_directory
115
+ local destination_directory
116
+ local directory_root
117
+
118
+ current_directory=$(get_path "$1")
119
+ destination_directory=$(get_path "$2")
120
+ directory_root=$(get_path "${3-$1}")
121
+
122
+ for child in "$current_directory"/*;
123
+ do
124
+ # Recurse $child directory
125
+ if [ -d "$child" ];
126
+ then
127
+ local file_path
128
+
129
+ [[ "$child" =~ "$directory_root"/(.*)$ ]] && file_path="${BASH_REMATCH[1]}"
130
+
131
+ mkdir -p "$destination_directory/$file_path"
132
+
133
+ local directory
134
+
135
+ directory="$child"
136
+
137
+ [[ $VERBOSE == true ]] && echo Recursing into "$directory"
138
+
139
+ decrypt_directory "$directory" "$destination_directory" "$directory_root"
140
+ else
141
+ # Ensure $child is a file (not "/*")
142
+ if [ -f "$child" ];
143
+ then
144
+ local file_name
145
+
146
+ [[ "$child" =~ "$directory_root"/(.*) ]] && file_name="${BASH_REMATCH[1]}"
147
+
148
+ mkdir -p "$destination_directory"
149
+
150
+ local file
151
+
152
+ file="$child"
153
+
154
+ [[ $VERBOSE == true ]] && echo Decrypting "$file"
155
+
156
+ decrypt "$file" > "$destination_directory/$file_name"
157
+ fi
158
+ fi
159
+ done
160
+ }
161
+
41
162
  export -f encrypt
42
163
 
43
164
  export -f decrypt
165
+
166
+ export -f encrypt_directory
167
+
168
+ export -f decrypt_directory
package/decrypt.sh CHANGED
@@ -16,5 +16,17 @@ then
16
16
  exit 1
17
17
  fi
18
18
 
19
+ if [ -d "$ORIGIN" ];
20
+ then
21
+ if [ -d "$DESTINATION" ];
22
+ then
23
+ decrypt_directory "$ORIGIN" "$DESTINATION"
24
+ exit 0
25
+ else
26
+ echo Origin is a directory but destination is not a directory
27
+ exit 1
28
+ fi
29
+ fi
30
+
19
31
  decrypt "$ORIGIN" > "$DESTINATION"
20
32
  exit 0
package/encrypt.sh CHANGED
@@ -16,5 +16,17 @@ then
16
16
  exit 1
17
17
  fi
18
18
 
19
+ if [ -d "$ORIGIN" ];
20
+ then
21
+ if [ -d "$DESTINATION" ];
22
+ then
23
+ encrypt_directory "$ORIGIN" "$DESTINATION"
24
+ exit 0
25
+ else
26
+ echo Origin is a directory but destination is not a directory
27
+ exit 1
28
+ fi
29
+ fi
30
+
19
31
  encrypt "$ORIGIN" > "$DESTINATION"
20
32
  exit 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sequencemedia/crypto",
3
- "version": "1.1.16",
3
+ "version": "1.1.18",
4
4
  "keywords": [
5
5
  "crypto",
6
6
  "node",
@@ -40,8 +40,8 @@
40
40
  "@babel/core": "^7.21.4",
41
41
  "@babel/eslint-parser": "^7.21.3",
42
42
  "@babel/preset-env": "^7.21.4",
43
- "@sequencemedia/hooks": "^1.0.392",
44
- "@types/node": "^18.15.12",
43
+ "@sequencemedia/hooks": "^1.0.393",
44
+ "@types/node": "^18.15.13",
45
45
  "core-js": "^3.30.1",
46
46
  "eslint": "^8.38.0",
47
47
  "eslint-config-standard": "^17.0.0",