zurik 1.0.2 → 1.0.5
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/bin/zurik.js +58 -5
- package/lib/crypto.js +11 -13
- package/lib/encrypt.js +4 -1
- package/package.json +1 -1
- package/readme.md +3 -0
package/bin/zurik.js
CHANGED
|
@@ -1,21 +1,74 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import * as readline from "readline";
|
|
3
4
|
import encryptFiles from "../lib/encrypt.js";
|
|
4
5
|
import decryptFiles from "../lib/decrypt.js";
|
|
5
6
|
|
|
6
7
|
const args = process.argv.slice(2);
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
function readPassword() {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
const rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout,
|
|
15
|
+
terminal: false
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
if (process.stdin.isTTY) {
|
|
19
|
+
process.stdin.setRawMode(true);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
process.stdout.write("Password: ");
|
|
23
|
+
|
|
24
|
+
let password = "";
|
|
25
|
+
|
|
26
|
+
process.stdin.on("data", (char) => {
|
|
27
|
+
char = char.toString();
|
|
28
|
+
|
|
29
|
+
if (char === "\n" || char === "\r" || char === "\u0004") {
|
|
30
|
+
process.stdin.pause();
|
|
31
|
+
if (process.stdin.isTTY) {
|
|
32
|
+
process.stdin.setRawMode(false);
|
|
33
|
+
}
|
|
34
|
+
rl.close();
|
|
35
|
+
console.log();
|
|
36
|
+
resolve(password);
|
|
37
|
+
} else if (char === "\u007f" || char === "\b") {
|
|
38
|
+
// Backspace
|
|
39
|
+
if (password.length > 0) {
|
|
40
|
+
password = password.slice(0, -1);
|
|
41
|
+
process.stdout.clearLine(0);
|
|
42
|
+
process.stdout.cursorTo(0);
|
|
43
|
+
process.stdout.write("Password: " + "*".repeat(password.length));
|
|
44
|
+
}
|
|
45
|
+
} else if (char === "\u0003") {
|
|
46
|
+
// Ctrl+C
|
|
47
|
+
console.log();
|
|
48
|
+
process.exit(0);
|
|
49
|
+
} else {
|
|
50
|
+
password += char;
|
|
51
|
+
process.stdout.write("*");
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
async function extractPassword() {
|
|
9
59
|
const pIndex = args.indexOf("-p");
|
|
10
|
-
if (pIndex !== -1
|
|
11
|
-
|
|
60
|
+
if (pIndex !== -1) {
|
|
61
|
+
if (args[pIndex + 1] && !args[pIndex + 1].startsWith("-")) {
|
|
62
|
+
return args[pIndex + 1];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return await readPassword()
|
|
12
66
|
}
|
|
13
67
|
return process.env.ZURIK_PASSWORD || null;
|
|
14
68
|
}
|
|
15
69
|
|
|
16
|
-
|
|
17
70
|
async function run() {
|
|
18
|
-
const password = extractPassword();
|
|
71
|
+
const password = await extractPassword();
|
|
19
72
|
if (!password) {
|
|
20
73
|
console.error("Missing password. Use -p <password> or set ZURIK_PASSWORD environment variable.");
|
|
21
74
|
process.exit(1);
|
package/lib/crypto.js
CHANGED
|
@@ -3,9 +3,9 @@ import crypto from "crypto";
|
|
|
3
3
|
const ALGO = "aes-256-gcm";
|
|
4
4
|
const IV_LENGTH = 16;
|
|
5
5
|
|
|
6
|
-
function deriveKey(password) {
|
|
6
|
+
function deriveKey(password, salt) {
|
|
7
7
|
return new Promise((resolve, reject) => {
|
|
8
|
-
crypto.scrypt(password,
|
|
8
|
+
crypto.scrypt(password, salt, 32, (err, key) => {
|
|
9
9
|
if (err) reject(err);
|
|
10
10
|
else resolve(key);
|
|
11
11
|
});
|
|
@@ -13,29 +13,27 @@ function deriveKey(password) {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
export async function encryptData(data, password) {
|
|
16
|
-
const
|
|
16
|
+
const salt = crypto.randomBytes(16);
|
|
17
|
+
const key = await deriveKey(password, salt);
|
|
17
18
|
const iv = crypto.randomBytes(IV_LENGTH);
|
|
18
19
|
const cipher = crypto.createCipheriv(ALGO, key, iv);
|
|
19
20
|
|
|
20
21
|
const encrypted = Buffer.concat([cipher.update(data), cipher.final()]);
|
|
21
22
|
const tag = cipher.getAuthTag();
|
|
22
23
|
|
|
23
|
-
return Buffer.concat([iv, tag, encrypted]).toString("base64");
|
|
24
|
+
return Buffer.concat([salt, iv, tag, encrypted]).toString("base64");
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
export async function decryptData(base64, password) {
|
|
27
28
|
const buffer = Buffer.from(base64, "base64");
|
|
29
|
+
const salt = buffer.subarray(0, 16);
|
|
30
|
+
const iv = buffer.subarray(16, 32);
|
|
31
|
+
const tag = buffer.subarray(32, 48);
|
|
32
|
+
const ciphertext = buffer.subarray(48);
|
|
28
33
|
|
|
29
|
-
const
|
|
30
|
-
const tag = buffer.subarray(16, 32);
|
|
31
|
-
const ciphertext = buffer.subarray(32);
|
|
32
|
-
|
|
33
|
-
const key = await deriveKey(password);
|
|
34
|
+
const key = await deriveKey(password, salt);
|
|
34
35
|
const decipher = crypto.createDecipheriv(ALGO, key, iv);
|
|
35
36
|
decipher.setAuthTag(tag);
|
|
36
37
|
|
|
37
|
-
return Buffer.concat([
|
|
38
|
-
decipher.update(ciphertext),
|
|
39
|
-
decipher.final()
|
|
40
|
-
]);
|
|
38
|
+
return Buffer.concat([decipher.update(ciphertext), decipher.final()]);
|
|
41
39
|
}
|
package/lib/encrypt.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
|
-
import path from "path";
|
|
3
2
|
import { encryptData } from "./crypto.js";
|
|
4
3
|
|
|
5
4
|
export default async function encryptFiles(password) {
|
|
@@ -15,6 +14,10 @@ export default async function encryptFiles(password) {
|
|
|
15
14
|
.map(x => x.trim())
|
|
16
15
|
.filter(Boolean);
|
|
17
16
|
|
|
17
|
+
if (fileList.length === 0) {
|
|
18
|
+
throw new Error("No files listed in .gitencrypt");
|
|
19
|
+
}
|
|
20
|
+
|
|
18
21
|
const bundle = { version: 1, files: {} };
|
|
19
22
|
|
|
20
23
|
for (const filePath of fileList) {
|
package/package.json
CHANGED