mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
remove browserify-cipher, use crypto.subtle for nip04.
This commit is contained in:
69
nip04.ts
69
nip04.ts
@@ -1,45 +1,66 @@
|
||||
import {Buffer} from 'buffer'
|
||||
import {randomBytes} from '@noble/hashes/utils'
|
||||
import * as secp256k1 from '@noble/secp256k1'
|
||||
// @ts-ignore
|
||||
import aes from 'browserify-cipher'
|
||||
import {encode as b64encode, decode as b64decode} from 'base64-arraybuffer'
|
||||
|
||||
export function encrypt(privkey: string, pubkey: string, text: string): string {
|
||||
import {utf8Decoder, utf8Encoder} from './utils'
|
||||
|
||||
export async function encrypt(
|
||||
privkey: string,
|
||||
pubkey: string,
|
||||
text: string
|
||||
): Promise<string> {
|
||||
const key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
const normalizedKey = getNormalizedX(key)
|
||||
|
||||
let iv = Uint8Array.from(randomBytes(16))
|
||||
var cipher = aes.createCipheriv(
|
||||
'aes-256-cbc',
|
||||
Buffer.from(normalizedKey, 'hex'),
|
||||
iv
|
||||
let plaintext = utf8Encoder.encode(text)
|
||||
let cryptoKey = await crypto.subtle.importKey(
|
||||
'raw',
|
||||
normalizedKey,
|
||||
{name: 'AES-CBC'},
|
||||
false,
|
||||
['encrypt']
|
||||
)
|
||||
let encryptedMessage = cipher.update(text, 'utf8', 'base64')
|
||||
encryptedMessage += cipher.final('base64')
|
||||
let ciphertext = await crypto.subtle.encrypt(
|
||||
{name: 'AES-CBC', iv},
|
||||
cryptoKey,
|
||||
plaintext
|
||||
)
|
||||
let ctb64 = b64encode(ciphertext)
|
||||
let ivb64 = b64encode(iv.buffer)
|
||||
|
||||
return `${encryptedMessage}?iv=${Buffer.from(iv.buffer).toString('base64')}`
|
||||
return `${ctb64}?iv=${ivb64}`
|
||||
}
|
||||
|
||||
export function decrypt(
|
||||
export async function decrypt(
|
||||
privkey: string,
|
||||
pubkey: string,
|
||||
ciphertext: string
|
||||
): string {
|
||||
let [cip, iv] = ciphertext.split('?iv=')
|
||||
data: string
|
||||
): Promise<string> {
|
||||
let [ctb64, ivb64] = data.split('?iv=')
|
||||
let key = secp256k1.getSharedSecret(privkey, '02' + pubkey)
|
||||
let normalizedKey = getNormalizedX(key)
|
||||
|
||||
var decipher = aes.createDecipheriv(
|
||||
'aes-256-cbc',
|
||||
Buffer.from(normalizedKey, 'hex'),
|
||||
Buffer.from(iv, 'base64')
|
||||
let cryptoKey = await crypto.subtle.importKey(
|
||||
'raw',
|
||||
normalizedKey,
|
||||
{name: 'AES-CBC'},
|
||||
false,
|
||||
['decrypt']
|
||||
)
|
||||
let decryptedMessage = decipher.update(cip, 'base64', 'utf8')
|
||||
decryptedMessage += decipher.final('utf8')
|
||||
let ciphertext = b64decode(ctb64)
|
||||
let iv = b64decode(ivb64)
|
||||
|
||||
return decryptedMessage
|
||||
let plaintext = await crypto.subtle.decrypt(
|
||||
{name: 'AES-CBC', iv},
|
||||
cryptoKey,
|
||||
ciphertext
|
||||
)
|
||||
|
||||
let text = utf8Decoder.decode(plaintext)
|
||||
return text
|
||||
}
|
||||
|
||||
function getNormalizedX(key: Uint8Array): string {
|
||||
return Buffer.from(key.slice(1, 33)).toString('hex')
|
||||
function getNormalizedX(key: Uint8Array): Uint8Array {
|
||||
return key.slice(1, 33)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user