Crypto API Reference
This document provides a concise API reference for the Crypto
class and its related interfaces, which provide cryptographic hashing functionality, specifically SHA-256 hashing, with compatibility across Node.js and React Native environments.
Types and Interfaces
CryptoModule
Interface representing a cryptographic module capable of creating hash instances or providing a direct SHA256 method.
-
createHash?
(optional method): Function accepting an algorithm name and returning aHash
instance. -
SHA256?
(optional method): Convenience method acceptingHashInput
and returning an object with atoString()
method to get the hash string.
Hash
Interface representing a hash computation instance.
-
update(data: HashInput): Hash
Updates the hash with the given data. Returns theHash
instance for chaining. -
digest(encoding: 'hex'): string
Finalizes the hash computation and returns the digest in the specified encoding format (only'hex'
supported here).
Class: Crypto
sha256
public static sha256(data: HashInput): string
Computes the SHA-256 hash of the given input data.
-
data
(required,HashInput
): The input data to hash. Supported types includestring
,Buffer
,Uint8Array
, andArrayBuffer
.
-
A hexadecimal string representing the SHA-256 hash of the input data.
-
Throws an error if no compatible crypto module can be found in the current environment.
-
Internally uses the first available crypto module in the following order:
-
Node.js built-in
crypto
module. -
react-native-quick-crypto
module (for React Native).
-
-
Converts various input types to a Node.js
Buffer
before hashing.
Internal Functions (not part of public API)
getAvailableCryptoModule
function getAvailableCryptoModule(): CryptoModule
Selects and returns a compatible cryptographic module available in the runtime environment.
-
The first compatible
CryptoModule
found (crypto
for Node.js orreact-native-quick-crypto
for React Native).
-
Throws an error if no compatible crypto module is found, prompting the user to install required polyfills.
convertInputToBuffer
function convertInputToBuffer(data: HashInput): Buffer
Converts the input data of various supported types into a Node.js Buffer
.
-
data
(required,HashInput
): The input data to convert.
-
A
Buffer
instance representing the input data in UTF-8 encoding (for strings) or raw bytes (for binary types).