-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsbot-util.js
76 lines (71 loc) · 1.99 KB
/
sbot-util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
const path = require('path')
const BoxStream = require('pull-box-stream')
const File = require('pull-file')
const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const crypto = require('crypto')
const zeros = new Buffer(24); zeros.fill(0)
const fs = require('fs')
const os = require('os')
const shortid = require('shortid')
function Hash (cb) {
let hash = crypto.createHash('sha256')
let buffers = []
let hasher = pull.drain(function (data) {
data = 'string' === typeof data ? new Buffer(data) : data
buffers.push(data)
hash.update(data)
}, function (err) {
cb(err, buffers, hash.digest())
})
return hasher
}
function boxBlob(filePath, sbot, cb) {
pull(File(filePath), Hash(function (err, buffers, key) {
if(err) cb(err)
pull(
pull.once(Buffer.concat(buffers)),
BoxStream.createBoxStream(key, zeros),
Hash(function (err, buffers, hash) {
if(err) cb(err)
var id = '&'+hash.toString('base64')+'.sha256'
pull(
pull.values(buffers),
sbot.blobs.add(id, function (err) {
if(err) cb(err)
sbot.blobs.push(id, function () {
cb(null, id+'?unbox='+key.toString('base64')+'.boxs')
})
})
)
})
)
}))
}
function unBoxBlob(blobId, sbot, cb) {
var id = blobId.split('?')[0]
var key = new Buffer(blobId.split('?')[1].replace(/^unbox=/,''), 'base64')
sbot.blobs.want(id, function (err, has) {
if(err) cb(err)
if(!has) {
return cb('could not retrive blob:'+id)
}
let inFilePath = path.join(os.tmpdir(), shortid.generate())
pull(
sbot.blobs.get(id),
BoxStream.createUnboxStream(key, zeros),
toPull.sink(fs.createWriteStream(inFilePath), function (err) {
if(err) {
console.log(err)
cb("Couldn't decrypt")
}
else cb(null, inFilePath)
})
)
})
}
module.exports = {
boxBlob,
unBoxBlob
}