Skip to content

Commit 62c03f6

Browse files
committed
String with bytes signature
Switch to allow default bitLen
1 parent d9a518e commit 62c03f6

File tree

4 files changed

+110
-111
lines changed

4 files changed

+110
-111
lines changed

dist/lib/entropy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var endianByteNum = function () {
4343
return buf8[0] === 0xff ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7];
4444
}();
4545

46-
var _stringWithBytes = function _stringWithBytes(bitLen, bytes, charset) {
46+
var _stringWithBytes = function _stringWithBytes(bytes, bitLen, charset) {
4747
if (bitLen <= 0) {
4848
return '';
4949
}
@@ -207,7 +207,7 @@ var _class = function () {
207207
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
208208

209209
var bytesNeeded = charset.bytesNeeded(bitLen);
210-
return this.stringWithBytes(bitLen, cryptoBytes(bytesNeeded), charset);
210+
return this.stringWithBytes(cryptoBytes(bytesNeeded), bitLen, charset);
211211
}
212212
}, {
213213
key: 'stringPRNG',
@@ -216,7 +216,7 @@ var _class = function () {
216216
var charset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : propMap.get(this).charset;
217217

218218
var bytesNeeded = charset.bytesNeeded(bitLen);
219-
return this.stringWithBytes(bitLen, randomBytes(bytesNeeded), charset);
219+
return this.stringWithBytes(randomBytes(bytesNeeded), bitLen, charset);
220220
}
221221

222222
/**
@@ -233,10 +233,10 @@ var _class = function () {
233233
}
234234
}, {
235235
key: 'stringWithBytes',
236-
value: function stringWithBytes(bitLen, bytes) {
236+
value: function stringWithBytes(bytes, bitLen) {
237237
var charset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : propMap.get(this).charset;
238238

239-
return _stringWithBytes(bitLen, bytes, charset);
239+
return _stringWithBytes(bytes, bitLen, charset);
240240
}
241241
}, {
242242
key: 'bytesNeeded',

examples/custom_bytes.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// Custom bytes
2-
32
const { default: Entropy } = require('./entropy')
43

5-
const entropy = new Entropy()
4+
const entropy = new Entropy({ bits: 30 })
65
const bytes = Buffer.from([250, 200, 150, 100])
7-
let string = entropy.stringWithBytes(30, bytes)
6+
let string = entropy.stringWithBytes(bytes)
87
console.log(`\n Custom bytes string : ${string}\n`)
98

109
try {
11-
string = entropy.stringWithBytes(32, bytes)
10+
string = entropy.stringWithBytes(bytes, 32)
1211
} catch (error) {
1312
console.log(` Error: ${error.message}\n`)
1413
}

lib/entropy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const endianByteNum = (() => {
2121
return (buf8[0] === 0xff) ? [2, 3, 4, 5, 6, 7] : [0, 1, 2, 3, 6, 7]
2222
})()
2323

24-
const stringWithBytes = (bitLen, bytes, charset) => {
24+
const stringWithBytes = (bytes, bitLen, charset) => {
2525
if (bitLen <= 0) { return '' }
2626

2727
const bitsPerChar = charset.getBitsPerChar()
@@ -154,12 +154,12 @@ export default class {
154154

155155
string(bitLen = propMap.get(this).bitLen, charset = propMap.get(this).charset) {
156156
const bytesNeeded = charset.bytesNeeded(bitLen)
157-
return this.stringWithBytes(bitLen, cryptoBytes(bytesNeeded), charset)
157+
return this.stringWithBytes(cryptoBytes(bytesNeeded), bitLen, charset)
158158
}
159159

160160
stringPRNG(bitLen = propMap.get(this).bitLen, charset = propMap.get(this).charset) {
161161
const bytesNeeded = charset.bytesNeeded(bitLen)
162-
return this.stringWithBytes(bitLen, randomBytes(bytesNeeded), charset)
162+
return this.stringWithBytes(randomBytes(bytesNeeded), bitLen, charset)
163163
}
164164

165165
/**
@@ -169,8 +169,8 @@ export default class {
169169
return this.stringPRNG(bitLen, charset)
170170
}
171171

172-
stringWithBytes(bitLen, bytes, charset = propMap.get(this).charset) {
173-
return stringWithBytes(bitLen, bytes, charset)
172+
stringWithBytes(bytes, bitLen, charset = propMap.get(this).charset) {
173+
return stringWithBytes(bytes, bitLen, charset)
174174
}
175175

176176
bytesNeeded(bitLen = propMap.get(this).bitLen, charset = propMap.get(this).charset) {

0 commit comments

Comments
 (0)