Skip to content

Commit 1a63680

Browse files
committed
adding proper types to index.d.ts, adding first itteration of PRNG. Neither CSPRNG nor PRNG are yet ready to use
1 parent db2241a commit 1a63680

3 files changed

Lines changed: 81 additions & 48 deletions

File tree

JSHash/lib/core/Random/PRNG.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
3+
// Generate a random integer r with equal chance in min <= r < max.
4+
const randrange = (min, max) => {
5+
const range = max - min;
6+
if (range <= 0) {
7+
throw new Error('max must be larger than min');
8+
}
9+
const requestBytes = Math.ceil(Math.log2(range) / 8);
10+
if (!requestBytes) { // No randomness required
11+
return min;
12+
}
13+
const maxNum = (256) ** (requestBytes);
14+
const ar = new Uint8Array(requestBytes);
15+
16+
while (true) {
17+
window.crypto.getRandomValues(ar);
18+
19+
let val = 0;
20+
for (let i = 0; i < requestBytes; i += 1) {
21+
val = (val << 8) + ar[i];
22+
}
23+
24+
if (val < (maxNum - maxNum) % range) {
25+
return min + (val % range);
26+
}
27+
}
28+
};
29+
30+
export default randrange;

JSHash/random.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* eslint linebreak-style: ["error", "windows"] */
2+
3+
export { default as PRNG } from './lib/core/Random/PRNG.js';

index.d.ts

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
1-
import RNHash from ".";
1+
declare namespace RNHash {
22

3-
export as namespace RNHash;
3+
function hashFile(uri: string, algorithm: string): Promise<string>;
4+
/**
5+
*
6+
* @param uri uri pointing to File or directory
7+
* @param algorithm algorithm to be used for hashing
8+
* @param minFileSize minimum file size to be hashed in bytes
9+
* @param maxFileSize maximum file size to be hashed in bytes
10+
* @param extensionFilter extension of files to be hashed, pass "" to ignore this option
11+
* @param batchSize event batch size, pass -1 to retrieve all results on .then instead of events
12+
* @returns Promise -- if batchSize is set to -1, the promise.resolve contains all the hashes, otherwise, it resolves to null.
13+
*/
14+
function hashFilesForFolder(
15+
uri: string, algorithm: string, minFileSize: number, maxFileSize: number, extensionFilter: string, batchSize: number, delay: number
16+
): Promise<{ FilesCount: number, isFinalBatch: boolean, batchNumber: number, results: Record<string, string> }>;
417

5-
export function hashFile(uri: string, algorithm: string): Promise<string>;
6-
/**
7-
*
8-
* @param uri uri pointing to File or directory
9-
* @param algorithm algorithm to be used for hashing
10-
* @param minFileSize minimum file size to be hashed in bytes
11-
* @param maxFileSize maximum file size to be hashed in bytes
12-
* @param extensionFilter extension of files to be hashed, pass "" to ignore this option
13-
* @param batchSize event batch size, pass -1 to retrieve all results on .then instead of events
14-
* @returns Promise -- if batchSize is set to -1, the promise.resolve contains all the hashes, otherwise, it resolves to null.
15-
*/
16-
export function hashFilesForFolder(
17-
uri: string, algorithm: string, minFileSize: number, maxFileSize: number, extensionFilter: string, batchSize: number, delay: number
18-
): Promise<{ FilesCount: number, isFinalBatch: bool, batchNumber: number, results: Record<string, string> }>;
18+
function hashFilesForFolders(
19+
uri: Array<string>, algorithm: string, minFileSize: number, maxFileSize: number, extensionFilter: string, batchSize: number, delay: number
20+
): Promise<{ FilesCount: number, isFinalBatch: boolean, batchNumber: number, results: Record<string, string> }>;
1921

20-
export function hashFilesForFolders(
21-
uri: Array<string>, algorithm: string, minFileSize: number, maxFileSize: number, extensionFilter: string, batchSize: number, delay: number
22-
): Promise<{ FilesCount: number, isFinalBatch: bool, batchNumber: number, results: Record<string, string> }>;
22+
23+
function hashUrl(url: string, HTTPMethod: string, headers: Record<string, string>, algorithm: string): Promise<string>;
24+
function hashString(message: string, algorithm: string): Promise<string>;
25+
function generateHmac(message: string, key: string, algorithm: string): Promise<string>;
26+
}
2327

2428

25-
export function hashUrl(url: string, HTTPMethod: string, headers: Record<string, string>, algorithm: string): Promise<string>;
26-
export function hashString(message: string, algorithm: string): Promise<string>;
27-
export function generateHmac(message: string, key: string, algorithm: string): Promise<string>;
2829

2930
export namespace CONSTANTS {
3031
export namespace HashAlgorithms {
3132
export const md2: string;
32-
export const md5: String;
33-
export const sha1: String;
34-
export const sha224: String;
35-
export const sha256: String;
36-
export const sha384: String;
37-
export const sha512: String;
38-
export const keccak: String;
33+
export const md5: string;
34+
export const sha1: string;
35+
export const sha224: string;
36+
export const sha256: string;
37+
export const sha384: string;
38+
export const sha512: string;
39+
export const keccak: string;
3940
}
4041
export namespace HmacAlgorithms {
41-
export const HmacMD5: String;
42-
export const HmacSHA1: String;
43-
export const HmacSHA224: String;
44-
export const HmacSHA256: String;
45-
export const HmacSHA384: String;
46-
export const HmacSHA512: String;
47-
export const PBEwithHmacSHA: String;
48-
export const PBEwithHmacSHA1: String;
49-
export const PBEwithHmacSHA224: String;
50-
export const PBEwithHmacSHA256: String;
51-
export const PBEwithHmacSHA384: String;
52-
export const PBEwithHmacSHA512: String;
42+
export const HmacMD5: string;
43+
export const HmacSHA1: string;
44+
export const HmacSHA224: string;
45+
export const HmacSHA256: string;
46+
export const HmacSHA384: string;
47+
export const HmacSHA512: string;
48+
export const PBEwithHmacSHA: string;
49+
export const PBEwithHmacSHA1: string;
50+
export const PBEwithHmacSHA224: string;
51+
export const PBEwithHmacSHA256: string;
52+
export const PBEwithHmacSHA384: string;
53+
export const PBEwithHmacSHA512: string;
5354
}
5455
export namespace Events {
55-
export const onBatchReccieved: String;
56+
export const onBatchReccieved: string;
5657
}
5758
}
5859

59-
export namespace JSHash {
60-
export function hashString(message: string, algorithm: string): Promise<string>;
61-
}
6260

63-
export namespace JSHmac {
64-
export function hashString(message: string, secret: string, algorithm: string): Promise<string>;
65-
}
61+
export default RNHash;
62+
63+
export function JSHash(message: string, algorithm: string): Promise<string>;
64+
65+
export function JSHmac(message: string, secret: string, algorithm: string): Promise<string>;

0 commit comments

Comments
 (0)