feat: Enhance Nostr chat encryption with key validation and logging
- Implement validation for private and public keys before encryption, ensuring they are present and in the correct hex format. - Add detailed console logging for key lengths and encryption success, improving debugging and transparency in the message encryption process. - Refactor the encryption call to use validated keys, enhancing security and reliability.
This commit is contained in:
parent
7241789c9e
commit
dc053ad1be
2 changed files with 50 additions and 8 deletions
|
|
@ -6,16 +6,26 @@
|
|||
* Convert hex string to Uint8Array
|
||||
*/
|
||||
export function hexToBytes(hex: string): Uint8Array {
|
||||
if (typeof hex !== 'string') {
|
||||
throw new TypeError('hexToBytes: expected string, got ' + typeof hex)
|
||||
}
|
||||
|
||||
if (hex.length % 2 !== 0) {
|
||||
throw new Error('Hex string must have even length')
|
||||
throw new Error('hexToBytes: received invalid unpadded hex')
|
||||
}
|
||||
|
||||
const bytes = hex.match(/.{2}/g)?.map(byte => parseInt(byte, 16))
|
||||
if (!bytes) {
|
||||
throw new Error('Invalid hex string')
|
||||
const array = new Uint8Array(hex.length / 2)
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
const j = i * 2
|
||||
const hexByte = hex.slice(j, j + 2)
|
||||
const byte = Number.parseInt(hexByte, 16)
|
||||
if (Number.isNaN(byte) || byte < 0) {
|
||||
throw new Error('Invalid byte sequence')
|
||||
}
|
||||
array[i] = byte
|
||||
}
|
||||
|
||||
return new Uint8Array(bytes)
|
||||
return array
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue