Skip to content

Commit

Permalink
docs: Apply syntax highlighing
Browse files Browse the repository at this point in the history
Closes #301.
  • Loading branch information
luczsoma committed Feb 14, 2020
1 parent df606ce commit 2d6fc00
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ TypeScript/JavaScript library for generating cryptographically strong, uniformly

Being an npm package, you can install crypto-random with the following command:

```
```bash
npm install -P @diplomatiq/crypto-random
```

## Testing

Run tests with the following:

```
```bash
npm test
```

Expand All @@ -82,15 +82,15 @@ _Note: This package is built as an ES6 package. You will not be able to use `req

After installation, import the `RandomGenerator` class into your project, and use its async API after instantiation:

```
```typescript
import { RandomGenerator } from '@diplomatiq/crypto-random';

//

async function main() {
const randomGenerator = new RandomGenerator();
const randomString = await randomGenerator.alphanumeric(32);
// randomString will contain a 32-character-long alphanumeric string
const randomGenerator = new RandomGenerator();
const randomString = await randomGenerator.alphanumeric(32);
// randomString will contain a 32-character-long alphanumeric string
}
```

Expand All @@ -104,7 +104,7 @@ The below referenced `MAX_ALPHABET_LEN` value determines the maximum number of e

### bytes(byteCount: number): Promise\<Uint8Array>;

```
```typescript
/**
* Returns an array of @param byteCount length filled with cryptographically strong random bytes.
*/
Expand All @@ -113,7 +113,7 @@ bytes(byteCount: number): Promise<Uint8Array>;

### integer(min: number, max: number, howMany = 1, unique = false): Promise\<number[]>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated positive integer between @param min and @param max,
* inclusive.
Expand All @@ -132,7 +132,7 @@ integer(min: number, max: number, howMany = 1, unique = false): Promise<number[]

### string(alphabet: string, desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated string value with a @param desiredLength length
* from a given @param alphabet.
Expand All @@ -144,7 +144,7 @@ string(alphabet: string, desiredLength: number, unique = false): Promise<string>

### lowercase(desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated string with lowercase letters only.
*/
Expand All @@ -153,7 +153,7 @@ lowercase(desiredLength: number, unique = false): Promise<string>;

### uppercase(desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated string with uppercase letters only.
*/
Expand All @@ -162,7 +162,7 @@ uppercase(desiredLength: number, unique = false): Promise<string>;

### numeric(desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated string with numeric characters only.
*/
Expand All @@ -171,7 +171,7 @@ numeric(desiredLength: number, unique = false): Promise<string>;

### alphabetic(desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated string with lower- and uppercase letters only.
*/
Expand All @@ -180,7 +180,7 @@ alphabetic(desiredLength: number, unique = false): Promise<string>;

### alphanumeric(desiredLength: number, unique = false): Promise\<string>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated alphanumeric string.
*/
Expand All @@ -189,7 +189,7 @@ alphanumeric(desiredLength: number, unique = false): Promise<string>;

### boolean(): Promise\<boolean>;

```
```typescript
/**
* Returns a cryptographically strong randomly generated boolean value.
*/
Expand All @@ -202,28 +202,28 @@ boolean(): Promise<boolean>;

Providing no arguments in the constructor, the `RandomGenerator` is instantiated using the default `BrowserEntropyProvider` as its entropy source. This will look for `window.crypto.getRandomValues`.

```
```typescript
type UnsignedTypedArray = Uint8Array | Uint16Array | Uint32Array;
```

```
```typescript
interface EntropyProvider {
getRandomValues<T extends UnsignedTypedArray>(array: T): T | Promise<T>;
getRandomValues<T extends UnsignedTypedArray>(array: T): T | Promise<T>;
}
```

```
```typescript
class RandomGenerator {
/**
* Provides entropy in the form of random-filled typed arrays.
*/
private readonly entropyProvider: EntropyProvider;
/**
* Provides entropy in the form of random-filled typed arrays.
*/
private readonly entropyProvider: EntropyProvider;

constructor(entropyProvider: EntropyProvider = new BrowserEntropyProvider()) {
this.entropyProvider = entropyProvider;
}
constructor(entropyProvider: EntropyProvider = new BrowserEntropyProvider()) {
this.entropyProvider = entropyProvider;
}

// …
//
}
```

Expand All @@ -233,38 +233,38 @@ You can inject any entropy source into the `RandomGenerator` as long as it imple

E.g. in your Node.js application, you can create `nodeJsEntropyProvider.ts`:

```
```typescript
import { EntropyProvider, UnsignedTypedArray } from '@diplomatiq/crypto-random';
import { randomFill } from 'crypto';

export class NodeJsEntropyProvider implements EntropyProvider {
public async getRandomValues<T extends UnsignedTypedArray>(array: T): Promise<T> {
return new Promise<T>((resolve, reject): void => {
randomFill(array, (error: Error | null, array: T) => {
if (error !== null) {
reject(error);
return;
}
resolve(array);
});
});
}
public async getRandomValues<T extends UnsignedTypedArray>(array: T): Promise<T> {
return new Promise<T>((resolve, reject): void => {
randomFill(array, (error: Error | null, array: T) => {
if (error !== null) {
reject(error);
return;
}
resolve(array);
});
});
}
}
```

And then (still in your Node.js application) use `RandomGenerator` as follows:

```
```typescript
import { RandomGenerator } from '@diplomatiq/crypto-random';
import { NodeJsEntropyProvider } from './nodeJsEntropyProvider';

//

async function main() {
const entropyProvider = new NodeJsEntropyProvider();
const randomGenerator = new RandomGenerator(entropyProvider);
const randomString = await randomGenerator.alphanumeric(32);
// randomString will contain a 32-character-long alphanumeric string
const entropyProvider = new NodeJsEntropyProvider();
const randomGenerator = new RandomGenerator(entropyProvider);
const randomString = await randomGenerator.alphanumeric(32);
// randomString will contain a 32-character-long alphanumeric string
}
```

Expand Down

0 comments on commit 2d6fc00

Please sign in to comment.