@@ -4,15 +4,17 @@ import { VirgilStreamVerifier } from '../../streams/VirgilStreamVerifier';
44import { VirgilPrivateKey } from '../../VirgilPrivateKey' ;
55import { VirgilPublicKey } from '../../VirgilPublicKey' ;
66import { createAsyncIterable , splitIntoChunks , createVirgilKeyPair } from './utils' ;
7+ import { VirgilStreamCipher } from '../../streams/VirgilStreamCipher' ;
8+ import { VirgilStreamDecipher } from '../../streams/VirgilStreamDecipher' ;
9+ import { StringEncoding } from '../../utils/anyToBuffer' ;
710
811const CHUNK_SIZE = 65536 ;
912
10- describe ( 'stream signing' , function ( ) {
13+ describe ( 'stream signing' , function ( ) {
1114 this . timeout ( 30 * 1000 ) ;
1215
13- describe ( 'signature calculation' , ( ) => {
14-
15- it ( 'calulates the signature' , async ( ) => {
16+ describe ( 'signature calculation' , ( ) => {
17+ it ( 'calculates the signature' , async ( ) => {
1618 const { privateKey } = createVirgilKeyPair ( ) ;
1719 const streamSigner = new VirgilStreamSigner ( ) ;
1820 const input = Buffer . alloc ( 3 * 1000 * 1000 ) . fill ( 'foo' ) ;
@@ -27,8 +29,78 @@ describe ('stream signing', function () {
2729 } ) ;
2830 } ) ;
2931
30- describe ( 'signature verification' , ( ) => {
32+ describe ( 'sign and encrypt, then decrypt and verify' , ( ) => {
33+ let publicKey : VirgilPublicKey ;
34+ let privateKey : VirgilPrivateKey ;
35+ let signature : Buffer ;
36+ let inputChunks : Buffer [ ] ;
37+
38+ beforeEach ( async ( ) => {
39+ ( { privateKey, publicKey } = createVirgilKeyPair ( ) ) ;
40+ const streamSigner = new VirgilStreamSigner ( ) ;
41+ const input = Buffer . alloc ( 3 * 1000 ) . fill ( 'foo' ) ;
42+ inputChunks = splitIntoChunks ( input , CHUNK_SIZE ) ;
43+
44+ for await ( const chunk of createAsyncIterable ( inputChunks ) ) {
45+ streamSigner . update ( chunk ) ;
46+ }
47+
48+ signature = streamSigner . sign ( privateKey ) ;
49+ } ) ;
50+
51+ const transferSignature = async ( encoding ?: StringEncoding ) => {
52+ assert . isTrue ( Buffer . isBuffer ( signature ) ) ;
53+
54+ const streamCipher = new VirgilStreamCipher (
55+ publicKey ,
56+ {
57+ encoding,
58+ signature : encoding ? signature . toString ( encoding ) : signature ,
59+ }
60+ ) ;
61+ const encryptedBuffer : Buffer [ ] = [ ] ;
62+
63+ encryptedBuffer . push ( streamCipher . start ( ) ) ;
64+
65+ for await ( const chunk of createAsyncIterable ( inputChunks ) ) {
66+ encryptedBuffer . push ( streamCipher . update ( chunk ) ) ;
67+ }
68+ encryptedBuffer . push ( streamCipher . final ( ) ) ;
69+
70+ const streamDecipher = new VirgilStreamDecipher ( privateKey ) ;
71+ const decryptedBuffer : Buffer [ ] = [ ] ;
72+
73+ for await ( const chunk of createAsyncIterable ( encryptedBuffer ) ) {
74+ decryptedBuffer . push ( streamDecipher . update ( chunk ) ) ;
75+ }
76+
77+ decryptedBuffer . push ( streamDecipher . final ( false ) ) ;
78+ let transferredSignature : Buffer | string = streamDecipher . getSignature ( ) ! ;
79+ assert . isNotEmpty ( transferredSignature ) ;
80+ if ( encoding ) transferredSignature = transferredSignature . toString ( encoding ) ;
81+ streamDecipher . dispose ( ) ;
82+ assert . exists ( transferredSignature ) ;
83+ const streamVerifier = new VirgilStreamVerifier ( transferredSignature ! , encoding ) ;
84+
85+ for await ( const chunk of createAsyncIterable ( inputChunks ) ) {
86+ streamVerifier . update ( chunk ) ;
87+ }
88+
89+ return streamVerifier . verify ( publicKey ) ;
90+ } ;
91+
92+ it ( 'transfer base64 signature' , async ( ) => {
93+ const isVerified = await transferSignature ( 'base64' ) ;
94+ assert . isTrue ( isVerified ) ;
95+ } ) ;
96+
97+ it ( 'transfer buffer signature' , async ( ) => {
98+ const isVerified = await transferSignature ( ) ;
99+ assert . isTrue ( isVerified ) ;
100+ } ) ;
101+ } ) ;
31102
103+ describe ( 'signature verification' , ( ) => {
32104 it ( 'verifies the signature' , async ( ) => {
33105 const keyPair = createVirgilKeyPair ( ) ;
34106 const streamSigner = new VirgilStreamSigner ( ) ;
0 commit comments