@@ -4,6 +4,7 @@ import { CodeError } from '@libp2p/interface/errors'
44import randomBytes from '../random-bytes.js'
55import * as utils from './rsa-utils.js'
66import type { JWKKeyPair } from './interface.js'
7+ import type { Uint8ArrayList } from 'uint8arraylist'
78
89const keypair = promisify ( crypto . generateKeyPair )
910
@@ -42,30 +43,56 @@ export async function unmarshalPrivateKey (key: JsonWebKey): Promise<JWKKeyPair>
4243
4344export { randomBytes as getRandomValues }
4445
45- export async function hashAndSign ( key : JsonWebKey , msg : Uint8Array ) : Promise < Uint8Array > {
46- return crypto . createSign ( 'RSA-SHA256' )
47- . update ( msg )
48- // @ts -expect-error node types are missing jwk as a format
49- . sign ( { format : 'jwk' , key } )
46+ export async function hashAndSign ( key : JsonWebKey , msg : Uint8Array | Uint8ArrayList ) : Promise < Uint8Array > {
47+ const hash = crypto . createSign ( 'RSA-SHA256' )
48+
49+ if ( msg instanceof Uint8Array ) {
50+ hash . update ( msg )
51+ } else {
52+ for ( const buf of msg ) {
53+ hash . update ( buf )
54+ }
55+ }
56+
57+ // @ts -expect-error node types are missing jwk as a format
58+ return hash . sign ( { format : 'jwk' , key } )
5059}
5160
52- export async function hashAndVerify ( key : JsonWebKey , sig : Uint8Array , msg : Uint8Array ) : Promise < boolean > {
53- return crypto . createVerify ( 'RSA-SHA256' )
54- . update ( msg )
55- // @ts -expect-error node types are missing jwk as a format
56- . verify ( { format : 'jwk' , key } , sig )
61+ export async function hashAndVerify ( key : JsonWebKey , sig : Uint8Array , msg : Uint8Array | Uint8ArrayList ) : Promise < boolean > {
62+ const hash = crypto . createVerify ( 'RSA-SHA256' )
63+
64+ if ( msg instanceof Uint8Array ) {
65+ hash . update ( msg )
66+ } else {
67+ for ( const buf of msg ) {
68+ hash . update ( buf )
69+ }
70+ }
71+
72+ // @ts -expect-error node types are missing jwk as a format
73+ return hash . verify ( { format : 'jwk' , key } , sig )
5774}
5875
5976const padding = crypto . constants . RSA_PKCS1_PADDING
6077
61- export function encrypt ( key : JsonWebKey , bytes : Uint8Array ) : Uint8Array {
62- // @ts -expect-error node types are missing jwk as a format
63- return crypto . publicEncrypt ( { format : 'jwk' , key, padding } , bytes )
78+ export function encrypt ( key : JsonWebKey , bytes : Uint8Array | Uint8ArrayList ) : Uint8Array {
79+ if ( bytes instanceof Uint8Array ) {
80+ // @ts -expect-error node types are missing jwk as a format
81+ return crypto . publicEncrypt ( { format : 'jwk' , key, padding } , bytes )
82+ } else {
83+ // @ts -expect-error node types are missing jwk as a format
84+ return crypto . publicEncrypt ( { format : 'jwk' , key, padding } , bytes . subarray ( ) )
85+ }
6486}
6587
66- export function decrypt ( key : JsonWebKey , bytes : Uint8Array ) : Uint8Array {
67- // @ts -expect-error node types are missing jwk as a format
68- return crypto . privateDecrypt ( { format : 'jwk' , key, padding } , bytes )
88+ export function decrypt ( key : JsonWebKey , bytes : Uint8Array | Uint8ArrayList ) : Uint8Array {
89+ if ( bytes instanceof Uint8Array ) {
90+ // @ts -expect-error node types are missing jwk as a format
91+ return crypto . privateDecrypt ( { format : 'jwk' , key, padding } , bytes )
92+ } else {
93+ // @ts -expect-error node types are missing jwk as a format
94+ return crypto . privateDecrypt ( { format : 'jwk' , key, padding } , bytes . subarray ( ) )
95+ }
6996}
7097
7198export function keySize ( jwk : JsonWebKey ) : number {
0 commit comments