/
VirgilStreamSigner.ts
60 lines (55 loc) · 1.83 KB
/
VirgilStreamSigner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { validatePrivateKey } from '../validators';
import { VirgilPrivateKey } from '../VirgilPrivateKey';
import { getPrivateKeyBytes } from '../privateKeyUtils';
import { VirgilStreamSignerBase } from './VirgilStreamSignerBase';
/**
* Class responsible for generating signatures of streams of data.
*/
export class VirgilStreamSigner extends VirgilStreamSignerBase {
/**
* Initializes a new instance of `VirgilStreamSigner`.
* `VirgilStreamSigner` objects are not meant to be created with the `new`
* operator, use {@link VirgilCrypto.createStreamSigner} to create an instance.
*
* @internal
*/
constructor () {
super();
this.seqSigner.startSigningSafe();
}
/**
* Signs the data collected by {@link VirgilStreamSigner.update} method
* and returns the signature.
*
* @param {VirgilPrivateKey} privateKey - The private key to use to
* calculate the signature.
* @param {boolen} [final] - Optional. Indicating whether to automatically
* free the memory occupied by internal {@link seqSigner} object in the
* browser.
* Default is `true`. Pass `false` if you need to calculate more than one
* signature of the same data with different keys.
*
* In node.js this argument is ignored because the memory will be freed by the
* garbage collector.
*
* @returns {Buffer} The signature
*/
sign(privateKey: VirgilPrivateKey, final: boolean = true) {
if (this.isDisposed()) {
throw new Error(
'Illegal state. The VirgilStreamSigner has been disposed. ' +
'Pass `false` as the second argument to the `sign` method ' +
'if you need to generate more than one signature.'
);
}
validatePrivateKey(privateKey);
const privateKeyValue = getPrivateKeyBytes(privateKey);
try {
return this.seqSigner.signSafe(privateKeyValue, Buffer.alloc(0));
} finally {
if (final) {
this.dispose();
}
}
}
}