diff options
Diffstat (limited to 'server/helpers/peertubeCrypto.js')
-rw-r--r-- | server/helpers/peertubeCrypto.js | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/server/helpers/peertubeCrypto.js b/server/helpers/peertubeCrypto.js index 7e65c43e1..3826ebaf6 100644 --- a/server/helpers/peertubeCrypto.js +++ b/server/helpers/peertubeCrypto.js | |||
@@ -1,18 +1,18 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | var config = require('config') | 3 | const config = require('config') |
4 | var crypto = require('crypto') | 4 | const crypto = require('crypto') |
5 | var fs = require('fs') | 5 | const fs = require('fs') |
6 | var openssl = require('openssl-wrapper') | 6 | const openssl = require('openssl-wrapper') |
7 | var path = require('path') | 7 | const path = require('path') |
8 | var ursa = require('ursa') | 8 | const ursa = require('ursa') |
9 | 9 | ||
10 | var logger = require('./logger') | 10 | const logger = require('./logger') |
11 | 11 | ||
12 | var certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) | 12 | const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) |
13 | var algorithm = 'aes-256-ctr' | 13 | const algorithm = 'aes-256-ctr' |
14 | 14 | ||
15 | var peertubeCrypto = { | 15 | const peertubeCrypto = { |
16 | checkSignature: checkSignature, | 16 | checkSignature: checkSignature, |
17 | createCertsIfNotExist: createCertsIfNotExist, | 17 | createCertsIfNotExist: createCertsIfNotExist, |
18 | decrypt: decrypt, | 18 | decrypt: decrypt, |
@@ -22,8 +22,8 @@ var peertubeCrypto = { | |||
22 | } | 22 | } |
23 | 23 | ||
24 | function checkSignature (public_key, raw_data, hex_signature) { | 24 | function checkSignature (public_key, raw_data, hex_signature) { |
25 | var crt = ursa.createPublicKey(public_key) | 25 | const crt = ursa.createPublicKey(public_key) |
26 | var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') | 26 | const is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') |
27 | return is_valid | 27 | return is_valid |
28 | } | 28 | } |
29 | 29 | ||
@@ -43,22 +43,22 @@ function decrypt (key, data, callback) { | |||
43 | fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { | 43 | fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { |
44 | if (err) return callback(err) | 44 | if (err) return callback(err) |
45 | 45 | ||
46 | var my_private_key = ursa.createPrivateKey(file) | 46 | const my_private_key = ursa.createPrivateKey(file) |
47 | var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8') | 47 | const decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8') |
48 | var decrypted_data = symetricDecrypt(data, decrypted_key) | 48 | const decrypted_data = symetricDecrypt(data, decrypted_key) |
49 | 49 | ||
50 | return callback(null, decrypted_data) | 50 | return callback(null, decrypted_data) |
51 | }) | 51 | }) |
52 | } | 52 | } |
53 | 53 | ||
54 | function encrypt (public_key, data, callback) { | 54 | function encrypt (public_key, data, callback) { |
55 | var crt = ursa.createPublicKey(public_key) | 55 | const crt = ursa.createPublicKey(public_key) |
56 | 56 | ||
57 | symetricEncrypt(data, function (err, dataEncrypted) { | 57 | symetricEncrypt(data, function (err, dataEncrypted) { |
58 | if (err) return callback(err) | 58 | if (err) return callback(err) |
59 | 59 | ||
60 | var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') | 60 | const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') |
61 | var encrypted = { | 61 | const encrypted = { |
62 | data: dataEncrypted.crypted, | 62 | data: dataEncrypted.crypted, |
63 | key: key | 63 | key: key |
64 | } | 64 | } |
@@ -72,8 +72,8 @@ function getCertDir () { | |||
72 | } | 72 | } |
73 | 73 | ||
74 | function sign (data) { | 74 | function sign (data) { |
75 | var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) | 75 | const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) |
76 | var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') | 76 | const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') |
77 | 77 | ||
78 | return signature | 78 | return signature |
79 | } | 79 | } |
@@ -93,7 +93,7 @@ function certsExist (callback) { | |||
93 | function createCerts (callback) { | 93 | function createCerts (callback) { |
94 | certsExist(function (exist) { | 94 | certsExist(function (exist) { |
95 | if (exist === true) { | 95 | if (exist === true) { |
96 | var string = 'Certs already exist.' | 96 | const string = 'Certs already exist.' |
97 | logger.warning(string) | 97 | logger.warning(string) |
98 | return callback(new Error(string)) | 98 | return callback(new Error(string)) |
99 | } | 99 | } |
@@ -129,8 +129,8 @@ function generatePassword (callback) { | |||
129 | } | 129 | } |
130 | 130 | ||
131 | function symetricDecrypt (text, password) { | 131 | function symetricDecrypt (text, password) { |
132 | var decipher = crypto.createDecipher(algorithm, password) | 132 | const decipher = crypto.createDecipher(algorithm, password) |
133 | var dec = decipher.update(text, 'hex', 'utf8') | 133 | let dec = decipher.update(text, 'hex', 'utf8') |
134 | dec += decipher.final('utf8') | 134 | dec += decipher.final('utf8') |
135 | return dec | 135 | return dec |
136 | } | 136 | } |
@@ -139,8 +139,8 @@ function symetricEncrypt (text, callback) { | |||
139 | generatePassword(function (err, password) { | 139 | generatePassword(function (err, password) { |
140 | if (err) return callback(err) | 140 | if (err) return callback(err) |
141 | 141 | ||
142 | var cipher = crypto.createCipher(algorithm, password) | 142 | const cipher = crypto.createCipher(algorithm, password) |
143 | var crypted = cipher.update(text, 'utf8', 'hex') | 143 | let crypted = cipher.update(text, 'utf8', 'hex') |
144 | crypted += cipher.final('hex') | 144 | crypted += cipher.final('hex') |
145 | callback(null, { crypted: crypted, password: password }) | 145 | callback(null, { crypted: crypted, password: password }) |
146 | }) | 146 | }) |