@@ -33,6 +33,15 @@ export const NobleEciesToEcies = (nobleEcies: NobleEcies): Ecies => {
3333 } ;
3434} ;
3535
36+ export const EciesToNobleEcies = ( ecies : Ecies ) : NobleEcies => {
37+ return {
38+ iv : new Uint8Array ( ecies . iv ) ,
39+ ephemPublicKey : new Uint8Array ( ecies . ephemPublicKey ) ,
40+ ciphertext : new Uint8Array ( ecies . ciphertext ) ,
41+ mac : new Uint8Array ( ecies . mac ) ,
42+ } ;
43+ } ;
44+
3645export const hmacSha256Sign = ( key : Uint8Array , msg : Uint8Array ) => {
3746 const mac = hmac ( sha256 , key , msg ) ;
3847 return mac ;
@@ -46,7 +55,7 @@ export function hmacSha256Verify(key: Uint8Array, msg: Uint8Array, sig: Uint8Arr
4655export const nobleEncrypt = async function (
4756 publicKeyTo : Uint8Array ,
4857 msg : Uint8Array ,
49- opts ?: { iv ?: Uint8Array ; ephemPrivateKey ?: Uint8Array ; padding ?: boolean }
58+ opts ?: { iv ?: Uint8Array ; ephemPrivateKey ?: Uint8Array }
5059) : Promise < NobleEcies > {
5160 const ephemPrivateKey = opts ?. ephemPrivateKey || utils . randomPrivateKey ( ) ;
5261 const ephemPublicKey = getPublicKey ( ephemPrivateKey , false ) ;
@@ -106,3 +115,19 @@ export const nobleDecrypt = async function (privateKey: Uint8Array, opts: NobleE
106115
107116 return decrypted ;
108117} ;
118+
119+ export const encrypt = async function (
120+ publicKeyTo : Buffer ,
121+ msg : Buffer ,
122+ opts ?: { iv ?: Buffer ; ephemPrivateKey ?: Buffer ; padding ?: boolean }
123+ ) : Promise < Ecies > {
124+ if ( opts ?. padding !== undefined ) throw new Error ( "padding opts is not supported" ) ;
125+ const nobleEcies = await nobleEncrypt ( publicKeyTo , msg , opts ) ;
126+ return NobleEciesToEcies ( nobleEcies ) ;
127+ } ;
128+
129+ export const decrypt = async function ( privateKey : Buffer , opts : Ecies , padding ?: boolean ) : Promise < Buffer > {
130+ const nobleEcies = EciesToNobleEcies ( opts ) ;
131+ const decrypted = await nobleDecrypt ( privateKey , nobleEcies , padding ) ;
132+ return Buffer . from ( decrypted ) ;
133+ } ;
0 commit comments