]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/peertube-crypto.js
Fix standard lint
[github/Chocobozzz/PeerTube.git] / server / helpers / peertube-crypto.js
CommitLineData
9f10b292
C
1'use strict'
2
26d7d31b 3const bcrypt = require('bcrypt')
f0f5567b
C
4const crypto = require('crypto')
5const fs = require('fs')
6const openssl = require('openssl-wrapper')
f0f5567b 7const ursa = require('ursa')
9f10b292 8
e861452f 9const constants = require('../initializers/constants')
f0f5567b 10const logger = require('./logger')
9f10b292 11
f0f5567b 12const peertubeCrypto = {
c4403b29
C
13 checkSignature,
14 comparePassword,
15 createCertsIfNotExist,
16 cryptPassword,
c4403b29 17 sign
9f10b292
C
18}
19
bc503c2a
C
20function checkSignature (publicKey, rawData, hexSignature) {
21 const crt = ursa.createPublicKey(publicKey)
22 const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
23 return isValid
9f10b292
C
24}
25
26d7d31b
C
26function comparePassword (plainPassword, hashPassword, callback) {
27 bcrypt.compare(plainPassword, hashPassword, function (err, isPasswordMatch) {
28 if (err) return callback(err)
29
30 return callback(null, isPasswordMatch)
31 })
32}
33
9f10b292
C
34function createCertsIfNotExist (callback) {
35 certsExist(function (exist) {
36 if (exist === true) {
37 return callback(null)
38 }
39
40 createCerts(function (err) {
41 return callback(err)
dac0a531 42 })
9f10b292
C
43 })
44}
dac0a531 45
26d7d31b
C
46function cryptPassword (password, callback) {
47 bcrypt.genSalt(constants.BCRYPT_SALT_SIZE, function (err, salt) {
48 if (err) return callback(err)
49
50 bcrypt.hash(password, salt, function (err, hash) {
51 return callback(err, hash)
52 })
53 })
54}
55
9f10b292 56function sign (data) {
e861452f 57 const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
f0f5567b 58 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
dac0a531 59
9f10b292
C
60 return signature
61}
dac0a531 62
9f10b292 63// ---------------------------------------------------------------------------
dac0a531 64
9f10b292 65module.exports = peertubeCrypto
dac0a531 66
9f10b292 67// ---------------------------------------------------------------------------
dac0a531 68
9f10b292 69function certsExist (callback) {
e861452f 70 fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) {
9f10b292
C
71 return callback(exists)
72 })
73}
74
75function createCerts (callback) {
76 certsExist(function (exist) {
77 if (exist === true) {
f0f5567b 78 const string = 'Certs already exist.'
9f10b292
C
79 logger.warning(string)
80 return callback(new Error(string))
81 }
82
83 logger.info('Generating a RSA key...')
e861452f
C
84
85 let options = {
86 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
87 '2048': false
88 }
89 openssl.exec('genrsa', options, function (err) {
9f10b292
C
90 if (err) {
91 logger.error('Cannot create private key on this pod.')
92 return callback(err)
dac0a531 93 }
9f10b292 94 logger.info('RSA key generated.')
dac0a531 95
e861452f
C
96 options = {
97 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
98 'pubout': true,
99 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub'
100 }
9f10b292 101 logger.info('Manage public key...')
e861452f 102 openssl.exec('rsa', options, function (err) {
dac0a531 103 if (err) {
9f10b292 104 logger.error('Cannot create public key on this pod.')
dac0a531
C
105 return callback(err)
106 }
dac0a531 107
9f10b292
C
108 logger.info('Public key managed.')
109 return callback(null)
110 })
dac0a531 111 })
9f10b292
C
112 })
113}