use @noble/ciphers instead of webcrypto on nip04.

This commit is contained in:
fiatjaf 2024-02-17 18:15:42 -03:00
parent db6baf2e6b
commit 84f9881812
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 8 additions and 23 deletions

View File

@ -1,18 +1,9 @@
import { test, expect } from 'bun:test' import { test, expect } from 'bun:test'
import crypto from 'node:crypto'
import { encrypt, decrypt } from './nip04.ts' import { encrypt, decrypt } from './nip04.ts'
import { getPublicKey, generateSecretKey } from './pure.ts' import { getPublicKey, generateSecretKey } from './pure.ts'
import { bytesToHex, hexToBytes } from '@noble/hashes/utils' import { bytesToHex, hexToBytes } from '@noble/hashes/utils'
try {
// @ts-ignore
// eslint-disable-next-line no-undef
globalThis.crypto = crypto
} catch (err) {
/***/
}
test('encrypt and decrypt message', async () => { test('encrypt and decrypt message', async () => {
let sk1 = generateSecretKey() let sk1 = generateSecretKey()
let sk2 = generateSecretKey() let sk2 = generateSecretKey()

View File

@ -1,15 +1,10 @@
import { bytesToHex, randomBytes } from '@noble/hashes/utils' import { bytesToHex, randomBytes } from '@noble/hashes/utils'
import { secp256k1 } from '@noble/curves/secp256k1' import { secp256k1 } from '@noble/curves/secp256k1'
import { cbc } from '@noble/ciphers/aes'
import { base64 } from '@scure/base' import { base64 } from '@scure/base'
import { utf8Decoder, utf8Encoder } from './utils.ts' import { utf8Decoder, utf8Encoder } from './utils.ts'
// @ts-ignore
if (typeof crypto !== 'undefined' && !crypto.subtle && crypto.webcrypto) {
// @ts-ignore
crypto.subtle = crypto.webcrypto.subtle
}
export async function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): Promise<string> { export async function encrypt(secretKey: string | Uint8Array, pubkey: string, text: string): Promise<string> {
const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey const privkey: string = secretKey instanceof Uint8Array ? bytesToHex(secretKey) : secretKey
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey) const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
@ -17,8 +12,9 @@ export async function encrypt(secretKey: string | Uint8Array, pubkey: string, te
let iv = Uint8Array.from(randomBytes(16)) let iv = Uint8Array.from(randomBytes(16))
let plaintext = utf8Encoder.encode(text) let plaintext = utf8Encoder.encode(text)
let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['encrypt'])
let ciphertext = await crypto.subtle.encrypt({ name: 'AES-CBC', iv }, cryptoKey, plaintext) let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext)
let ctb64 = base64.encode(new Uint8Array(ciphertext)) let ctb64 = base64.encode(new Uint8Array(ciphertext))
let ivb64 = base64.encode(new Uint8Array(iv.buffer)) let ivb64 = base64.encode(new Uint8Array(iv.buffer))
@ -31,14 +27,12 @@ export async function decrypt(secretKey: string | Uint8Array, pubkey: string, da
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey) let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
let normalizedKey = getNormalizedX(key) let normalizedKey = getNormalizedX(key)
let cryptoKey = await crypto.subtle.importKey('raw', normalizedKey, { name: 'AES-CBC' }, false, ['decrypt'])
let ciphertext = base64.decode(ctb64)
let iv = base64.decode(ivb64) let iv = base64.decode(ivb64)
let ciphertext = base64.decode(ctb64)
let plaintext = await crypto.subtle.decrypt({ name: 'AES-CBC', iv }, cryptoKey, ciphertext) let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext)
let text = utf8Decoder.decode(plaintext) return utf8Decoder.decode(plaintext)
return text
} }
function getNormalizedX(key: Uint8Array): Uint8Array { function getNormalizedX(key: Uint8Array): Uint8Array {

View File

@ -208,7 +208,7 @@
}, },
"license": "Unlicense", "license": "Unlicense",
"dependencies": { "dependencies": {
"@noble/ciphers": "0.2.0", "@noble/ciphers": "^0.5.1",
"@noble/curves": "1.2.0", "@noble/curves": "1.2.0",
"@noble/hashes": "1.3.1", "@noble/hashes": "1.3.1",
"@scure/base": "1.1.1", "@scure/base": "1.1.1",