-
Notifications
You must be signed in to change notification settings - Fork 0
/
decrypt.mjs
34 lines (26 loc) · 979 Bytes
/
decrypt.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { SerialPort } from "serialport";
import { openCli, readUntil } from "./utils.mjs";
const U2F_KEY_SLOT = 11;
export default async function decrypt(path, iv, content) {
const port = new SerialPort({
path,
baudRate: 115200,
});
port.on("close", () => console.log("💔 Connection lost"));
console.log("👋 Trying to connect...");
console.log(
"🦄 Reset Flipper by pressing Arrow Left + Back buttons at the same time"
);
await openCli(port);
port.write(`crypto decrypt ${U2F_KEY_SLOT} ${iv}\r\n`);
await readUntil(port, "decryption:");
port.write(`${content}\x03`);
const decrypted = await readUntil(port, ">:");
const suffix = Buffer.from("\r\n\r\n>: ", "ascii").toString("hex");
const preamble = Buffer.from("\r\nDecrypted data:\r\n", "ascii").toString(
"hex"
);
const start = decrypted.indexOf(preamble) + preamble.length;
const end = decrypted.indexOf(suffix, start);
return decrypted.substring(start, end);
}