mirror of
https://github.com/p-stream/p-stream.git
synced 2026-03-11 17:55:33 +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;
|
||||
};
|
||||
|
||||
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) {
|
||||
return pbkdf2Async(sha256, mnemonic, "mnemonic", {
|
||||
c: 2048,
|
||||
|
|
@ -29,8 +37,8 @@ export async function keysFromMnemonic(mnemonic: string): Promise<Keys> {
|
|||
});
|
||||
|
||||
return {
|
||||
privateKey,
|
||||
publicKey,
|
||||
privateKey: new Uint8Array(privateKey),
|
||||
publicKey: new Uint8Array(publicKey),
|
||||
seed,
|
||||
};
|
||||
}
|
||||
|
|
@ -43,11 +51,12 @@ export async function signCode(
|
|||
code: string,
|
||||
privateKey: Uint8Array,
|
||||
): Promise<Uint8Array> {
|
||||
return forge.pki.ed25519.sign({
|
||||
const signature = forge.pki.ed25519.sign({
|
||||
encoding: "utf8",
|
||||
message: code,
|
||||
privateKey,
|
||||
privateKey: uint8ArrayToBuffer(privateKey),
|
||||
});
|
||||
return new Uint8Array(signature);
|
||||
}
|
||||
|
||||
export function bytesToBase64(bytes: Uint8Array) {
|
||||
|
|
@ -71,7 +80,9 @@ export function base64ToBuffer(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) {
|
||||
|
|
@ -91,7 +102,7 @@ export async function encryptData(data: string, secret: Uint8Array) {
|
|||
|
||||
const cipher = forge.cipher.createCipher(
|
||||
"AES-GCM",
|
||||
forge.util.createBuffer(secret),
|
||||
uint8ArrayToBuffer(secret),
|
||||
);
|
||||
cipher.start({
|
||||
iv,
|
||||
|
|
@ -115,7 +126,7 @@ export function decryptData(data: string, secret: Uint8Array) {
|
|||
|
||||
const decipher = forge.cipher.createDecipher(
|
||||
"AES-GCM",
|
||||
forge.util.createBuffer(secret),
|
||||
uint8ArrayToBuffer(secret),
|
||||
);
|
||||
decipher.start({
|
||||
iv: base64ToStringBuffer(iv),
|
||||
|
|
|
|||
Loading…
Reference in a new issue