Skip to content

Commit d8d96c8

Browse files
committed
use nacl
1 parent 0603ec0 commit d8d96c8

File tree

3 files changed

+34
-31
lines changed

3 files changed

+34
-31
lines changed

deno.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"react": "^18.3.1",
3030
"semver": "^7.6.3",
3131
"tiny-invariant": "^1.3.3",
32+
"tweetnacl": "^1.0.3",
33+
"tweetnacl-util": "^0.15.1",
3234
"yaml": "^2.6.1"
3335
},
3436
"devDependencies": {

src/lib/clusters/keys.tsx

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as crypto from "node:crypto";
1+
import { box, BoxKeyPair, randomBytes, } from "tweetnacl";
2+
import { encodeBase64, decodeBase64 } from "tweetnacl-util";
23
import * as path from "node:path";
34
import * as os from "node:os";
45
import { Buffer } from "node:buffer";
@@ -18,47 +19,37 @@ export async function getKeys(): Promise<{ publicKey: string; privateKey: string
1819
}
1920

2021
function generateKeyPair() {
21-
// Generate RSA key pair
22-
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
23-
modulusLength: 4096,
24-
publicKeyEncoding: {
25-
type: 'spki',
26-
format: 'pem'
27-
},
28-
privateKeyEncoding: {
29-
type: 'pkcs8',
30-
format: 'pem'
31-
}
32-
});
22+
// generate a key pair
23+
const pair = box.keyPair();
24+
const publicKey = encodeBase64(pair.publicKey);
25+
const privateKey = encodeBase64(pair.secretKey);
3326

3427
return {
3528
publicKey,
3629
privateKey,
3730
};
3831
}
3932

40-
export function decryptSecret(encrypted_secret: string, privateKey: string) {
41-
try {
42-
const decoded = Buffer.from(encrypted_secret, 'base64');
43-
const decrypted = crypto.privateDecrypt({
44-
key: privateKey,
45-
padding: crypto.constants.RSA_PKCS1_PADDING,
46-
}, decoded);
47-
48-
// Convert decrypted array to Buffer
49-
const decryptedBuffer = Buffer.isBuffer(decrypted) ? decrypted : Buffer.from(decrypted);
50-
51-
return decryptedBuffer.toString('utf8');
52-
} catch (err) {
53-
throw new Error(`Failed to decrypt secret: ${err}`);
33+
export function decryptSecret(props: { encrypted: string, secretKey: string, nonce: string, ephemeralKey: string }) {
34+
// Generate nonce and message from encrypted secret
35+
const decrypted = box.open(
36+
decodeBase64(props.encrypted),
37+
decodeBase64(props.nonce),
38+
decodeBase64(props.secretKey),
39+
decodeBase64(props.ephemeralKey)
40+
);
41+
42+
if (!decrypted) {
43+
throw new Error("Failed to decrypt secret");
5444
}
45+
return Buffer.from(decrypted).toString('utf8');
5546
}
5647

5748

5849
async function saveKeys(keys: { publicKey: string; privateKey: string }) {
5950
const { publicKey, privateKey } = keys;
60-
const publicKeyPath = path.join(os.homedir(), ".sf", "public.pem");
61-
const privateKeyPath = path.join(os.homedir(), ".sf", "private.pem");
51+
const publicKeyPath = path.join(os.homedir(), ".sf", "public_key");
52+
const privateKeyPath = path.join(os.homedir(), ".sf", "private_key");
6253

6354
try {
6455
// Create .sf directory if it doesn't exist
@@ -76,8 +67,8 @@ async function saveKeys(keys: { publicKey: string; privateKey: string }) {
7667
}
7768

7869
async function loadKeys() {
79-
const publicKeyPath = path.join(os.homedir(), ".sf", "public.pem");
80-
const privateKeyPath = path.join(os.homedir(), ".sf", "private.pem");
70+
const publicKeyPath = path.join(os.homedir(), ".sf", "public_key");
71+
const privateKeyPath = path.join(os.homedir(), ".sf", "private_key");
8172

8273
let publicKey = null;
8374
let privateKey = null;

0 commit comments

Comments
 (0)