1 #ifndef SRC_CRYPTO_CRYPTO_HASH_H_ 2 #define SRC_CRYPTO_CRYPTO_HASH_H_ 3 4 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 5 6 #include "base_object.h" 7 #include "crypto/crypto_keys.h" 8 #include "crypto/crypto_util.h" 9 #include "env.h" 10 #include "memory_tracker.h" 11 #include "v8.h" 12 13 namespace node { 14 namespace crypto { 15 class Hash final : public BaseObject { 16 public: 17 static void Initialize(Environment* env, v8::Local<v8::Object> target); 18 static void RegisterExternalReferences(ExternalReferenceRegistry* registry); 19 20 void MemoryInfo(MemoryTracker* tracker) const override; 21 SET_MEMORY_INFO_NAME(Hash) 22 SET_SELF_SIZE(Hash) 23 24 bool HashInit(const EVP_MD* md, v8::Maybe<unsigned int> xof_md_len); 25 bool HashUpdate(const char* data, size_t len); 26 27 static void GetHashes(const v8::FunctionCallbackInfo<v8::Value>& args); 28 29 protected: 30 static void New(const v8::FunctionCallbackInfo<v8::Value>& args); 31 static void HashUpdate(const v8::FunctionCallbackInfo<v8::Value>& args); 32 static void HashDigest(const v8::FunctionCallbackInfo<v8::Value>& args); 33 34 Hash(Environment* env, v8::Local<v8::Object> wrap); 35 36 private: 37 EVPMDPointer mdctx_ {}; 38 unsigned int md_len_ = 0; 39 ByteSource digest_; 40 }; 41 42 struct HashConfig final : public MemoryRetainer { 43 CryptoJobMode mode; 44 ByteSource in; 45 const EVP_MD* digest; 46 unsigned int length; 47 48 HashConfig() = default; 49 50 explicit HashConfig(HashConfig&& other) noexcept; 51 52 HashConfig& operator=(HashConfig&& other) noexcept; 53 54 void MemoryInfo(MemoryTracker* tracker) const override; 55 SET_MEMORY_INFO_NAME(HashConfig) 56 SET_SELF_SIZE(HashConfig) 57 }; 58 59 struct HashTraits final { 60 using AdditionalParameters = HashConfig; 61 static constexpr const char* JobName = "HashJob"; 62 static constexpr AsyncWrap::ProviderType Provider = 63 AsyncWrap::PROVIDER_HASHREQUEST; 64 65 static v8::Maybe<bool> AdditionalConfig( 66 CryptoJobMode mode, 67 const v8::FunctionCallbackInfo<v8::Value>& args, 68 unsigned int offset, 69 HashConfig* params); 70 71 static bool DeriveBits( 72 Environment* env, 73 const HashConfig& params, 74 ByteSource* out); 75 76 static v8::Maybe<bool> EncodeOutput( 77 Environment* env, 78 const HashConfig& params, 79 ByteSource* out, 80 v8::Local<v8::Value>* result); 81 }; 82 83 using HashJob = DeriveBitsJob<HashTraits>; 84 85 void InternalVerifyIntegrity(const v8::FunctionCallbackInfo<v8::Value>& args); 86 87 } // namespace crypto 88 } // namespace node 89 90 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS 91 #endif // SRC_CRYPTO_CRYPTO_HASH_H_ 92