1 import { BCRYPT_SALT_SIZE, PRIVATE_RSA_KEY_SIZE } from '../initializers'
2 import { ActorModel } from '../models/activitypub/actor'
3 import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey } from './core-utils'
4 import { jsig } from './custom-jsonld-signature'
5 import { logger } from './logger'
7 async function createPrivateAndPublicKeys () {
8 logger.info('Generating a RSA key...')
10 const { key } = await createPrivateKey(PRIVATE_RSA_KEY_SIZE)
11 const { publicKey } = await getPublicKey(key)
13 return { privateKey: key, publicKey }
16 function isSignatureVerified (fromActor: ActorModel, signedDocument: object) {
17 const publicKeyObject = {
18 '@context': jsig.SECURITY_CONTEXT_URL,
20 '@type': 'CryptographicKey',
22 publicKeyPem: fromActor.publicKey
25 const publicKeyOwnerObject = {
26 '@context': jsig.SECURITY_CONTEXT_URL,
28 publicKey: [ publicKeyObject ]
32 publicKey: publicKeyObject,
33 publicKeyOwner: publicKeyOwnerObject
36 return jsig.promises.verify(signedDocument, options)
38 logger.error('Cannot check signature.', { err })
43 function signObject (byActor: ActorModel, data: any) {
45 privateKeyPem: byActor.privateKey,
47 algorithm: 'RsaSignature2017'
50 return jsig.promises.sign(data, options)
53 function comparePassword (plainPassword: string, hashPassword: string) {
54 return bcryptComparePromise(plainPassword, hashPassword)
57 async function cryptPassword (password: string) {
58 const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE)
60 return bcryptHashPromise(password, salt)
63 // ---------------------------------------------------------------------------
68 createPrivateAndPublicKeys,