aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/peertubeCrypto.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-03-16 22:29:27 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-03-16 22:29:27 +0100
commitf0f5567b6918fc60c8cab15e13aec03a89a91dfb (patch)
tree99dfdb9fa8273c9cda1360fd3b6bfccc515bf8be /server/helpers/peertubeCrypto.js
parent5101105ef91bfe478f97546b78b321882da2079c (diff)
downloadPeerTube-f0f5567b6918fc60c8cab15e13aec03a89a91dfb.tar.gz
PeerTube-f0f5567b6918fc60c8cab15e13aec03a89a91dfb.tar.zst
PeerTube-f0f5567b6918fc60c8cab15e13aec03a89a91dfb.zip
Use const/let now we use node 4.2
Diffstat (limited to 'server/helpers/peertubeCrypto.js')
-rw-r--r--server/helpers/peertubeCrypto.js50
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
3var config = require('config') 3const config = require('config')
4var crypto = require('crypto') 4const crypto = require('crypto')
5var fs = require('fs') 5const fs = require('fs')
6var openssl = require('openssl-wrapper') 6const openssl = require('openssl-wrapper')
7var path = require('path') 7const path = require('path')
8var ursa = require('ursa') 8const ursa = require('ursa')
9 9
10var logger = require('./logger') 10const logger = require('./logger')
11 11
12var certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) 12const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
13var algorithm = 'aes-256-ctr' 13const algorithm = 'aes-256-ctr'
14 14
15var peertubeCrypto = { 15const 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
24function checkSignature (public_key, raw_data, hex_signature) { 24function 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
54function encrypt (public_key, data, callback) { 54function 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
74function sign (data) { 74function 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) {
93function createCerts (callback) { 93function 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
131function symetricDecrypt (text, password) { 131function 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 })