Skip to content

Commit

Permalink
Fix return value of Crypto#getRandomValues()
Browse files Browse the repository at this point in the history
It should return the same array passed to it [1].

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#Return_value

Change-Id: I4d7095d762bc50f4d5170d61aad8440634bbe91e
  • Loading branch information
cpetrov authored and tbuschto committed Oct 1, 2020
1 parent fe22c64 commit 699a9f6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/tabris/Crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class Crypto extends NativeObject {
throw new Error('Not enough random bytes available');
}
new Uint8Array(typedArray.buffer).set(values);
return typedArray;
}

}
Expand Down
7 changes: 7 additions & 0 deletions test/tabris/Crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ describe('Crypto', function() {
expect(buffer[2]).to.equal(0xffffffff);
});

it('returns given array', function() {
const buffer = new Uint32Array(3);
returnValue = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255]);

expect(crypto.getRandomValues(buffer)).to.equal(buffer);
});

});

});
10 changes: 4 additions & 6 deletions typings/global/crypto.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
declare class Crypto {

getRandomValues(
typedArray: Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array
): void;
type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array;

declare class Crypto {
getRandomValues(typedArray: TypedArray): TypedArray;
}

declare var crypto: Crypto;
declare var crypto: Crypto;

0 comments on commit 699a9f6

Please sign in to comment.