We can not sensibly parse both hex and base64, assume all input is hex
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import { ed25519, x25519 } from '@noble/curves/ed25519.js';
|
||||
import { sha256 } from "@noble/hashes/sha2.js";
|
||||
import { hmac } from '@noble/hashes/hmac.js';
|
||||
import { ecb, ecb, encrypt } from '@noble/ciphers/aes.js';
|
||||
import { bytesToHex, equalBytes, hexToBytes, encodedStringToBytes } from "./parser";
|
||||
import { ecb } from '@noble/ciphers/aes.js';
|
||||
import { bytesToHex, equalBytes, hexToBytes } from "./parser";
|
||||
import { IPublicKey, ISharedSecret, IStaticSecret } from './crypto.types';
|
||||
import { NodeHash } from './identity.types';
|
||||
|
||||
const PUBLIC_KEY_SIZE = 32;
|
||||
const SEED_SIZE = 32;
|
||||
const PRIVATE_KEY_SIZE = 32;
|
||||
const HMAC_SIZE = 2;
|
||||
const SHARED_SECRET_SIZE = 32;
|
||||
const SIGNATURE_SIZE = 64;
|
||||
@@ -19,7 +18,7 @@ export class PublicKey implements IPublicKey {
|
||||
|
||||
constructor(key: Uint8Array | string) {
|
||||
if (typeof key === 'string') {
|
||||
this.key = encodedStringToBytes(key, PUBLIC_KEY_SIZE);
|
||||
this.key = hexToBytes(key, PUBLIC_KEY_SIZE);
|
||||
} else if (key instanceof Uint8Array) {
|
||||
this.key = key;
|
||||
} else {
|
||||
@@ -46,7 +45,7 @@ export class PublicKey implements IPublicKey {
|
||||
} else if (other instanceof Uint8Array) {
|
||||
otherKey = other;
|
||||
} else if (typeof other === 'string') {
|
||||
otherKey = encodedStringToBytes(other, PUBLIC_KEY_SIZE);
|
||||
otherKey = hexToBytes(other, PUBLIC_KEY_SIZE);
|
||||
} else {
|
||||
throw new Error('Invalid type for PublicKey comparison');
|
||||
}
|
||||
@@ -67,7 +66,7 @@ export class PrivateKey {
|
||||
|
||||
constructor(seed: Uint8Array | string) {
|
||||
if (typeof seed === 'string') {
|
||||
seed = encodedStringToBytes(seed, SEED_SIZE);
|
||||
seed = hexToBytes(seed, SEED_SIZE);
|
||||
}
|
||||
if (seed.length !== SEED_SIZE) {
|
||||
throw new Error(`Invalid seed length: expected ${SEED_SIZE} bytes, got ${seed.length}`);
|
||||
@@ -165,15 +164,6 @@ export class SharedSecret implements ISharedSecret {
|
||||
return plaintext.slice(0, end);
|
||||
}
|
||||
|
||||
private zeroPad(data: Uint8Array): Uint8Array {
|
||||
if (data.length % 16 === 0) {
|
||||
return data;
|
||||
}
|
||||
const padded = new Uint8Array(Math.ceil(data.length / 16) * 16);
|
||||
padded.set(data);
|
||||
return padded;
|
||||
}
|
||||
|
||||
public encrypt(data: Uint8Array): { hmac: Uint8Array, ciphertext: Uint8Array } {
|
||||
const key = this.secret.slice(0, 16);
|
||||
const cipher = ecb(key, { disablePadding: true });
|
||||
@@ -204,7 +194,7 @@ export class SharedSecret implements ISharedSecret {
|
||||
|
||||
static fromName(name: string): SharedSecret {
|
||||
if (name === "Public") {
|
||||
return new SharedSecret(hexToBytes("8b3387e9c5cdea6ac9e5edbaa115cd72"));
|
||||
return new SharedSecret(hexToBytes("8b3387e9c5cdea6ac9e5edbaa115cd72", 16));
|
||||
} else if (!/^#/.test(name)) {
|
||||
throw new Error("Only the 'Public' group or groups starting with '#' are supported");
|
||||
}
|
||||
@@ -218,7 +208,7 @@ export class StaticSecret implements IStaticSecret {
|
||||
|
||||
constructor(secret: Uint8Array | string) {
|
||||
if (typeof secret === 'string') {
|
||||
secret = encodedStringToBytes(secret, STATIC_SECRET_SIZE);
|
||||
secret = hexToBytes(secret, STATIC_SECRET_SIZE);
|
||||
}
|
||||
if (secret.length !== STATIC_SECRET_SIZE) {
|
||||
throw new Error(`Invalid static secret length: expected ${STATIC_SECRET_SIZE} bytes, got ${secret.length}`);
|
||||
|
||||
Reference in New Issue
Block a user