Commit d5a075ec0b62f472d55f353de0a53d05c5a99845
Merge pull request #10 from dominictarr/master
allow key load/create calls to take config objectPaul Frazee committed on 3/6/2015, 5:18:11 PM
Parent: a9f960514483cbb32536d7c65a6056fd9c1933bd
Parent: dbfcdc3e700706a37cf8e462ae1d1560d58ac44f
Files changed
index.js | changed |
index.js | ||
---|---|---|
@@ -26,8 +26,12 @@ | ||
26 | 26 | function isHash (data) { |
27 | 27 | return isString(data) && /^[A-Za-z0-9\/+]{43}=\.blake2s$/.test(data) |
28 | 28 | } |
29 | 29 | |
30 | +function isObject (o) { | |
31 | + return 'object' === typeof o | |
32 | +} | |
33 | + | |
30 | 34 | exports.isHash = isHash |
31 | 35 | exports.hash = hash |
32 | 36 | |
33 | 37 | function isString(s) { |
@@ -101,21 +105,30 @@ | ||
101 | 105 | function tag (key, tag) { |
102 | 106 | return key.toString('base64')+'.' + tag.replace(/^\./, '') |
103 | 107 | } |
104 | 108 | |
109 | +var toNameFile = exports.toNameFile = function (namefile) { | |
110 | + if(isObject(namefile)) | |
111 | + return path.join(namefile.path, 'secret') | |
112 | + return namefile | |
113 | +} | |
114 | + | |
105 | 115 | exports.load = function(namefile, cb) { |
116 | + namefile = toNameFile(namefile) | |
106 | 117 | fs.readFile(namefile, 'ascii', function(err, privateKeyStr) { |
107 | 118 | if (err) return cb(err) |
108 | 119 | try { cb(null, reconstructKeys(privateKeyStr)) } |
109 | 120 | catch (e) { cb(err) } |
110 | 121 | }) |
111 | 122 | } |
112 | 123 | |
113 | 124 | exports.loadSync = function(namefile) { |
125 | + namefile = toNameFile(namefile) | |
114 | 126 | return reconstructKeys(fs.readFileSync(namefile, 'ascii')) |
115 | 127 | } |
116 | 128 | |
117 | 129 | exports.create = function(namefile, cb) { |
130 | + namefile = toNameFile(namefile) | |
118 | 131 | var k = constructKeys() |
119 | 132 | mkdirp(path.dirname(namefile), function (err) { |
120 | 133 | if(err) return cb(err) |
121 | 134 | fs.writeFile(namefile, k.keyfile, function(err) { |
@@ -126,22 +139,25 @@ | ||
126 | 139 | }) |
127 | 140 | } |
128 | 141 | |
129 | 142 | exports.createSync = function(namefile) { |
143 | + namefile = toNameFile(namefile) | |
130 | 144 | var k = constructKeys() |
131 | 145 | mkdirp.sync(path.dirname(namefile)) |
132 | 146 | fs.writeFileSync(namefile, k.keyfile) |
133 | 147 | delete k.keyfile |
134 | 148 | return k |
135 | 149 | } |
136 | 150 | |
137 | 151 | exports.loadOrCreate = function (namefile, cb) { |
152 | + namefile = toNameFile(namefile) | |
138 | 153 | exports.load(namefile, function (err, keys) { |
139 | 154 | if(!err) return cb(null, keys) |
140 | 155 | exports.create(namefile, cb) |
141 | 156 | }) |
142 | 157 | } |
143 | 158 | exports.loadOrCreateSync = function (namefile) { |
159 | + namefile = toNameFile(namefile) | |
144 | 160 | try { |
145 | 161 | return exports.loadSync(namefile) |
146 | 162 | } catch (err) { |
147 | 163 | return exports.createSync(namefile) |
@@ -210,5 +226,5 @@ | ||
210 | 226 | delete obj.hmac |
211 | 227 | var str = JSON.stringify(obj, null, 2) |
212 | 228 | var _hmac = exports.hmac(str, secret) |
213 | 229 | return deepEqual(hmac, _hmac) |
214 | -} | |
230 | +} |
Built with git-ssb-web