mirror of
https://github.com/p-stream/p-stream.git
synced 2026-04-21 11:02:18 +00:00
Fix crypto errors
This commit is contained in:
parent
ee247aacdd
commit
52292f11aa
1 changed files with 18 additions and 7 deletions
|
|
@ -10,6 +10,14 @@ type Keys = {
|
||||||
seed: Uint8Array;
|
seed: Uint8Array;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function uint8ArrayToBuffer(array: Uint8Array): forge.util.ByteStringBuffer {
|
||||||
|
return forge.util.createBuffer(
|
||||||
|
Array.from(array)
|
||||||
|
.map((byte) => String.fromCharCode(byte))
|
||||||
|
.join(""),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async function seedFromMnemonic(mnemonic: string) {
|
async function seedFromMnemonic(mnemonic: string) {
|
||||||
return pbkdf2Async(sha256, mnemonic, "mnemonic", {
|
return pbkdf2Async(sha256, mnemonic, "mnemonic", {
|
||||||
c: 2048,
|
c: 2048,
|
||||||
|
|
@ -29,8 +37,8 @@ export async function keysFromMnemonic(mnemonic: string): Promise<Keys> {
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
privateKey,
|
privateKey: new Uint8Array(privateKey),
|
||||||
publicKey,
|
publicKey: new Uint8Array(publicKey),
|
||||||
seed,
|
seed,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -43,11 +51,12 @@ export async function signCode(
|
||||||
code: string,
|
code: string,
|
||||||
privateKey: Uint8Array,
|
privateKey: Uint8Array,
|
||||||
): Promise<Uint8Array> {
|
): Promise<Uint8Array> {
|
||||||
return forge.pki.ed25519.sign({
|
const signature = forge.pki.ed25519.sign({
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
message: code,
|
message: code,
|
||||||
privateKey,
|
privateKey: uint8ArrayToBuffer(privateKey),
|
||||||
});
|
});
|
||||||
|
return new Uint8Array(signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function bytesToBase64(bytes: Uint8Array) {
|
export function bytesToBase64(bytes: Uint8Array) {
|
||||||
|
|
@ -71,7 +80,9 @@ export function base64ToBuffer(data: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function base64ToStringBuffer(data: string) {
|
export function base64ToStringBuffer(data: string) {
|
||||||
return forge.util.createBuffer(base64ToBuffer(data));
|
const decoded = base64ToBuffer(data);
|
||||||
|
|
||||||
|
return uint8ArrayToBuffer(decoded);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function stringBufferToBase64(buffer: forge.util.ByteStringBuffer) {
|
export function stringBufferToBase64(buffer: forge.util.ByteStringBuffer) {
|
||||||
|
|
@ -91,7 +102,7 @@ export async function encryptData(data: string, secret: Uint8Array) {
|
||||||
|
|
||||||
const cipher = forge.cipher.createCipher(
|
const cipher = forge.cipher.createCipher(
|
||||||
"AES-GCM",
|
"AES-GCM",
|
||||||
forge.util.createBuffer(secret),
|
uint8ArrayToBuffer(secret),
|
||||||
);
|
);
|
||||||
cipher.start({
|
cipher.start({
|
||||||
iv,
|
iv,
|
||||||
|
|
@ -115,7 +126,7 @@ export function decryptData(data: string, secret: Uint8Array) {
|
||||||
|
|
||||||
const decipher = forge.cipher.createDecipher(
|
const decipher = forge.cipher.createDecipher(
|
||||||
"AES-GCM",
|
"AES-GCM",
|
||||||
forge.util.createBuffer(secret),
|
uint8ArrayToBuffer(secret),
|
||||||
);
|
);
|
||||||
decipher.start({
|
decipher.start({
|
||||||
iv: base64ToStringBuffer(iv),
|
iv: base64ToStringBuffer(iv),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue