]>
Commit | Line | Data |
---|---|---|
e4f97bab | 1 | import * as jsig from 'jsonld-signatures' |
65fcc311 C |
2 | |
3 | import { | |
e4f97bab C |
4 | PRIVATE_RSA_KEY_SIZE, |
5 | BCRYPT_SALT_SIZE | |
65fcc311 | 6 | } from '../initializers' |
6fcd19ba | 7 | import { |
6fcd19ba C |
8 | bcryptComparePromise, |
9 | bcryptGenSaltPromise, | |
10 | bcryptHashPromise, | |
e4f97bab C |
11 | createPrivateKey, |
12 | getPublicKey, | |
13 | jsonldSignPromise, | |
14 | jsonldVerifyPromise | |
6fcd19ba | 15 | } from './core-utils' |
65fcc311 | 16 | import { logger } from './logger' |
e4f97bab | 17 | import { AccountInstance } from '../models/account/account-interface' |
9f10b292 | 18 | |
e4f97bab C |
19 | async function createPrivateAndPublicKeys () { |
20 | logger.info('Generating a RSA key...') | |
bdfbd4f1 | 21 | |
e4f97bab C |
22 | const { key } = await createPrivateKey(PRIVATE_RSA_KEY_SIZE) |
23 | const { publicKey } = await getPublicKey(key) | |
bdfbd4f1 | 24 | |
e4f97bab | 25 | return { privateKey: key, publicKey } |
9f10b292 C |
26 | } |
27 | ||
e4f97bab C |
28 | function isSignatureVerified (fromAccount: AccountInstance, signedDocument: object) { |
29 | const publicKeyObject = { | |
30 | '@context': jsig.SECURITY_CONTEXT_URL, | |
31 | '@id': fromAccount.url, | |
32 | '@type': 'CryptographicKey', | |
33 | owner: fromAccount.url, | |
34 | publicKeyPem: fromAccount.publicKey | |
bdfbd4f1 C |
35 | } |
36 | ||
e4f97bab C |
37 | const publicKeyOwnerObject = { |
38 | '@context': jsig.SECURITY_CONTEXT_URL, | |
39 | '@id': fromAccount.url, | |
40 | publicKey: [ publicKeyObject ] | |
41 | } | |
bdfbd4f1 | 42 | |
e4f97bab C |
43 | const options = { |
44 | publicKey: publicKeyObject, | |
45 | publicKeyOwner: publicKeyOwnerObject | |
46 | } | |
bdfbd4f1 | 47 | |
e4f97bab C |
48 | return jsonldVerifyPromise(signedDocument, options) |
49 | .catch(err => { | |
50 | logger.error('Cannot check signature.', err) | |
51 | return false | |
52 | }) | |
26d7d31b C |
53 | } |
54 | ||
e4f97bab C |
55 | function signObject (byAccount: AccountInstance, data: any) { |
56 | const options = { | |
57 | privateKeyPem: byAccount.privateKey, | |
58 | creator: byAccount.url | |
f5028693 | 59 | } |
9f10b292 | 60 | |
e4f97bab C |
61 | return jsonldSignPromise(data, options) |
62 | } | |
63 | ||
64 | function comparePassword (plainPassword: string, hashPassword: string) { | |
65 | return bcryptComparePromise(plainPassword, hashPassword) | |
9f10b292 | 66 | } |
dac0a531 | 67 | |
f5028693 C |
68 | async function cryptPassword (password: string) { |
69 | const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE) | |
70 | ||
53abc4c2 | 71 | return bcryptHashPromise(password, salt) |
26d7d31b C |
72 | } |
73 | ||
9f10b292 | 74 | // --------------------------------------------------------------------------- |
dac0a531 | 75 | |
65fcc311 | 76 | export { |
e4f97bab | 77 | isSignatureVerified, |
65fcc311 | 78 | comparePassword, |
e4f97bab | 79 | createPrivateAndPublicKeys, |
65fcc311 | 80 | cryptPassword, |
e4f97bab | 81 | signObject |
9f10b292 | 82 | } |