A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/openpgpjs/openpgpjs/commit/4d96089f72fb2b604d48d3001a4a7bb16901fc25 below:

OP-01-001 Type confusion in crypto.random.RandomBuffer (Low) · openpgpjs/openpgpjs@4d96089 · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+22

-6

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+22

-6

lines changed Original file line number Diff line number Diff line change

@@ -76,6 +76,9 @@ module.exports = {

76 76

* @param {Uint8Array} buf

77 77

*/

78 78

getRandomValues: function(buf) {

79 +

if (!(buf instanceof Uint8Array)) {

80 +

throw new Error('Invalid type: buf not an Uint8Array');

81 +

}

79 82

if (typeof window !== 'undefined' && window.crypto) {

80 83

window.crypto.getRandomValues(buf);

81 84

} else if (nodeCrypto) {

@@ -142,18 +145,21 @@ function RandomBuffer() {

142 145

* @param {Integer} size size of buffer

143 146

*/

144 147

RandomBuffer.prototype.init = function(size) {

145 -

this.buffer = new Uint32Array(size);

148 +

this.buffer = new Uint8Array(size);

146 149

this.size = 0;

147 150

};

148 151 149 152

/**

150 153

* Concat array of secure random numbers to buffer

151 -

* @param {Uint32Array} buf

154 +

* @param {Uint8Array} buf

152 155

*/

153 156

RandomBuffer.prototype.set = function(buf) {

154 157

if (!this.buffer) {

155 158

throw new Error('RandomBuffer is not initialized');

156 159

}

160 +

if (!(buf instanceof Uint8Array)) {

161 +

throw new Error('Invalid type: buf not an Uint8Array');

162 +

}

157 163

var freeSpace = this.buffer.length - this.size;

158 164

if (buf.length > freeSpace) {

159 165

buf = buf.subarray(0, freeSpace);

@@ -164,12 +170,15 @@ RandomBuffer.prototype.set = function(buf) {

164 170 165 171

/**

166 172

* Take numbers out of buffer and copy to array

167 -

* @param {Uint32Array} buf the destination array

173 +

* @param {Uint8Array} buf the destination array

168 174

*/

169 175

RandomBuffer.prototype.get = function(buf) {

170 176

if (!this.buffer) {

171 177

throw new Error('RandomBuffer is not initialized');

172 178

}

179 +

if (!(buf instanceof Uint8Array)) {

180 +

throw new Error('Invalid type: buf not an Uint8Array');

181 +

}

173 182

if (this.size < buf.length) {

174 183

throw new Error('Random number buffer depleted.')

175 184

}

Original file line number Diff line number Diff line change

@@ -31,6 +31,9 @@ onmessage = function (event) {

31 31

correct = false;

32 32

switch (msg.event) {

33 33

case 'seed-random':

34 +

if (!(msg.buf instanceof Uint8Array)) {

35 +

msg.buf = new Uint8Array(msg.buf);

36 +

}

34 37

window.openpgp.crypto.random.randomBuffer.set(msg.buf);

35 38

break;

36 39

case 'encrypt-message':

Original file line number Diff line number Diff line change

@@ -509,6 +509,8 @@ describe('Random Buffer', function() {

509 509

it('Set Method', function () {

510 510

randomBuffer.init(5);

511 511

var buf = new Uint32Array(2);

512 +

expect(randomBuffer.set.bind(randomBuffer, buf)).to.throw('Invalid type: buf not an Uint8Array');

513 +

buf = new Uint8Array(2);

512 514

buf[0] = 1; buf[1] = 2;

513 515

randomBuffer.set(buf);

514 516

expect(equal(randomBuffer.buffer, [1,2,0,0,0])).to.be.true;

@@ -520,7 +522,7 @@ describe('Random Buffer', function() {

520 522

expect(equal(randomBuffer.buffer, [1,2,1,2,1])).to.be.true;

521 523

expect(randomBuffer.size).to.equal(5);

522 524

randomBuffer.init(1);

523 -

var buf = new Uint32Array(2);

525 +

buf = new Uint8Array(2);

524 526

buf[0] = 1; buf[1] = 2;

525 527

randomBuffer.set(buf);

526 528

expect(buf).to.to.have.property('0', 1);

@@ -529,10 +531,12 @@ describe('Random Buffer', function() {

529 531 530 532

it('Get Method', function () {

531 533

randomBuffer.init(5);

532 -

var buf = new Uint32Array(5);

534 +

var buf = new Uint8Array(5);

533 535

buf[0] = 1; buf[1] = 2; buf[2] = 5; buf[3] = 7; buf[4] = 8;

534 536

randomBuffer.set(buf);

535 -

var buf = new Uint32Array(2);

537 +

buf = new Uint32Array(2);

538 +

expect(randomBuffer.get.bind(randomBuffer, buf)).to.throw('Invalid type: buf not an Uint8Array');

539 +

buf = new Uint8Array(2);

536 540

randomBuffer.get(buf);

537 541

expect(equal(randomBuffer.buffer, [1,2,5,7,8])).to.be.true;

538 542

expect(randomBuffer.size).to.equal(3);

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4