1- // Copyright 2022 Margelo
1+ // Copyright 2022 Margelo
22
33#include " HmacHostObject.h"
44
55#include < jsi/jsi.h>
6- #include < string>
76#include < openssl/hmac.h>
7+
8+ #include < string>
89#include < vector>
10+
911#include " ../../../../Downloads/cpp/JSI Utils/TypedArray.h"
1012
1113#define OUT
@@ -24,65 +26,57 @@ const EVP_MD* parseHashAlgorithm(const std::string& hashAlgorithm) {
2426 if (hashAlgorithm == " sha512" ) {
2527 return EVP_sha512 ();
2628 }
27- const EVP_MD* res = EVP_get_digestbyname (hashAlgorithm.c_str ());
28- if (res != nullptr ) {
29- return res;
30- }
31- throw std::runtime_error (" Invalid Hash Algorithm!" );
29+ const EVP_MD* res = EVP_get_digestbyname (hashAlgorithm.c_str ());
30+ if (res != nullptr ) {
31+ return res;
32+ }
33+ throw std::runtime_error (" Invalid Hash Algorithm!" );
3234}
3335
34- HmacHostObject::HmacHostObject (const std::string& hashAlgorithm,
35- jsi::Runtime & runtime,
36- jsi::ArrayBuffer & key,
37- std::shared_ptr<react::CallInvoker> jsCallInvoker,
38- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) :
39- SmartHostObject (jsCallInvoker, workerQueue) {
36+ HmacHostObject::HmacHostObject (
37+ const std::string& hashAlgorithm, jsi::Runtime& runtime,
38+ jsi::ArrayBuffer& key, std::shared_ptr<react::CallInvoker> jsCallInvoker,
39+ std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue)
40+ : SmartHostObject(jsCallInvoker, workerQueue) {
4041 this ->context = HMAC_CTX_new ();
4142 if (key.size (runtime) == 0 ) {
42- HMAC_Init_ex (this ->context ,
43- " " ,
44- 0 ,
45- parseHashAlgorithm (hashAlgorithm),
46- nullptr );
43+ HMAC_Init_ex (this ->context , " " , 0 , parseHashAlgorithm (hashAlgorithm),
44+ nullptr );
4745 } else {
48- HMAC_Init_ex (this ->context ,
49- key.data (runtime),
50- static_cast <int >(key.size (runtime)),
51- parseHashAlgorithm (hashAlgorithm),
52- nullptr );
46+ HMAC_Init_ex (this ->context , key.data (runtime),
47+ static_cast <int >(key.size (runtime)),
48+ parseHashAlgorithm (hashAlgorithm), nullptr );
5349 }
5450
55-
5651 this ->fields .push_back (HOST_LAMBDA (" update" , {
57- if (!arguments[0 ].isObject () || !arguments[0 ].getObject (runtime).isArrayBuffer (runtime)) {
58- throw jsi::JSError (runtime, " HmacHostObject::update: First argument ('message') has to be of type ArrayBuffer!" );
59- }
52+ if (!arguments[0 ].isObject () ||
53+ !arguments[0 ].getObject (runtime).isArrayBuffer (runtime)) {
54+ throw jsi::JSError (runtime,
55+ " HmacHostObject::update: First argument ('message') "
56+ " has to be of type ArrayBuffer!" );
57+ }
6058
61- auto message = arguments[0 ].getObject (runtime).getArrayBuffer (runtime);
59+ auto message = arguments[0 ].getObject (runtime).getArrayBuffer (runtime);
6260
63- HMAC_Update (this ->context ,
64- message.data (runtime),
65- message.size (runtime));
61+ HMAC_Update (this ->context , message.data (runtime), message.size (runtime));
6662
67- return jsi::Value::undefined ();
68- }));
63+ return jsi::Value::undefined ();
64+ }));
6965
7066 this ->fields .push_back (HOST_LAMBDA (" digest" , {
71- auto size = HMAC_size (this ->context );
67+ auto size = HMAC_size (this ->context );
7268
73- unsigned char * OUT md = new unsigned char [size];
74- unsigned int OUT length;
69+ unsigned char * OUT md = new unsigned char [size];
70+ unsigned int OUT length;
7571
76- HMAC_Final (this ->context ,
77- md,
78- &length);
72+ HMAC_Final (this ->context , md, &length);
7973
80- TypedArray<TypedArrayKind::Uint8Array> typedArray (runtime, length);
81- std::vector<unsigned char > vec (md, md + length);
82- typedArray.update (runtime, vec);
74+ TypedArray<TypedArrayKind::Uint8Array> typedArray (runtime, length);
75+ std::vector<unsigned char > vec (md, md + length);
76+ typedArray.update (runtime, vec);
8377
84- return typedArray;
85- }));
78+ return typedArray;
79+ }));
8680}
8781
8882HmacHostObject::~HmacHostObject () {
@@ -91,4 +85,4 @@ HmacHostObject::~HmacHostObject() {
9185 }
9286}
9387
94- }
88+ } // namespace margelo
0 commit comments