add generateKeyFromPassword

This commit is contained in:
Pavel Prichodko 2022-06-04 13:09:31 +02:00 committed by Felicio Mununga
parent 7bebcc9d30
commit 6e97051de9
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,24 @@
import { bytesToHex } from 'ethereum-cryptography/utils'
import { generateKeyFromPassword } from './generate-key-from-password'
describe('createSymKeyFromPassword', () => {
it('should create symmetric key from password', async () => {
const password = 'password'
const symKey = await generateKeyFromPassword(password)
expect(bytesToHex(symKey)).toEqual(
'c49ad65ebf2a7b7253bf400e3d27719362a91b2c9b9f54d50a69117021666c33'
)
})
it('should generate symmetric key from chat ID', async () => {
const chatId =
'0x02dcec6041fb999d65f1d33363e08c93d3c1f6f0fbbb26add383e2cf46c2b921f41dc14fd8-9a8b-4df5-a358-2c3067be5439'
const symKey = await generateKeyFromPassword(chatId)
expect(bytesToHex(symKey)).toEqual(
'76ff5bf0a74a8e724367c7fc003f066d477641f468768a8da2817addf5c2ce76'
)
})
})

View File

@ -0,0 +1,19 @@
import { pbkdf2 } from 'ethereum-cryptography/pbkdf2'
import { hexToBytes, utf8ToBytes } from 'ethereum-cryptography/utils'
const AES_KEY_LENGTH = 32 // bytes
/**
* status-go: https://github.com/status-im/status-go/blob/a471fed6a64e01a1aba8d925377fba045a5aa9f9/wakuv2/waku.go#L713
*/
export async function generateKeyFromPassword(
password: string
): Promise<Uint8Array> {
return await pbkdf2(
hexToBytes(password),
utf8ToBytes(''),
65356,
AES_KEY_LENGTH,
'sha256'
)
}