Js_of_ocaml.CryptoWeb Crypto API.
A code example:
let hash data =
Promise.map
(fun buf -> buf)
((Crypto.subtle ())##digest
(Crypto.algorithm (Js.string "SHA-256"))
(Crypto.buffer_source_of_array_buffer_view data))Most of SubtleCrypto is exposed as object methods, mirroring the JavaScript surface: obtain the subtle object with subtle and call its methods with ##.
An AlgorithmIdentifier: either a bare name (e.g. "SHA-256") or a parameter object (e.g. {name: "AES-GCM"; iv: ...}). Build one with algorithm or algorithm_obj.
val algorithm : Js.js_string Js.t -> algorithmalgorithm name is the bare-name form of an algorithm identifier, such as algorithm (Js.string "SHA-256").
algorithm_obj o uses an arbitrary parameter object as an algorithm identifier. Build o with Js.Unsafe.obj or the object%js syntax.
A BufferSource: an ArrayBuffer or any ArrayBufferView (typed array or DataView). Build one with buffer_source_of_array_buffer or buffer_source_of_array_buffer_view.
val buffer_source_of_array_buffer :
Typed_array.arrayBuffer Js.t ->
bufferSourceval buffer_source_of_array_buffer_view :
Typed_array.arrayBufferView Js.t ->
bufferSourcemodule Key_format : sig ... endA KeyFormat for importing and exporting keys.
class type cryptoKey = object ... endclass type cryptoKeyPair = object ... endtype jsonWebKey = Js.Unsafe.anyA key in JsonWebKey form (RFC 7517): a plain JSON object whose members carry the key material (e.g. kty, n, e for RSA; crv, x, y for EC). Exposed as Js.Unsafe.any: read its members with Js.Unsafe.get, build one with Js.Unsafe.obj, and serialise/parse with the JSON global.
type params = | RSASSA_PKCS1_v1_5Sign/verify; parameters are just the name.
*)| RSA_PSS of {}| RSA_OAEP of {label : bufferSource option;}| ECDSA of {hash : algorithm;}| ECDH of {}| AES_CTR of {counter : bufferSource;length : int;}| AES_CBC of {iv : bufferSource;}| AES_GCM of {iv : bufferSource;additional_data : bufferSource option;tag_length : int option;}| AES_KWKey-wrapping; parameters are just the name.
*)| HMACSign/verify; parameters are just the name.
*)| HKDF of {hash : algorithm;salt : bufferSource;info : bufferSource;}| PBKDF2 of {hash : algorithm;salt : bufferSource;iterations : int;}| RSA_keygen of {name : Js.js_string Js.t;"RSASSA-PKCS1-v1_5", "RSA-PSS" or "RSA-OAEP".
modulus_length : int;public_exponent : Typed_array.uint8Array Js.t;hash : algorithm;}| RSA_import of {name : Js.js_string Js.t;"RSASSA-PKCS1-v1_5", "RSA-PSS" or "RSA-OAEP".
hash : algorithm;}| EC_keygen of {name : Js.js_string Js.t;"ECDSA" or "ECDH".
named_curve : Js.js_string Js.t;}EcKeyGenParams.
| AES_keygen of {name : Js.js_string Js.t;"AES-CTR", "AES-CBC", "AES-GCM" or "AES-KW".
length : int;}AesKeyGenParams.
| HMAC_keygen of {hash : algorithm;length : int option;}HmacKeyGenParams.
| SHA_1| SHA_256| SHA_384| SHA_512| Other of Js.js_string Js.tAn algorithm whose name is not recognised.
*)A typed view of an algorithm identifier, with one constructor per WebCrypto algorithm-parameter dictionary. The nullary constructors map to a bare name string; the others map to a parameter object. hash fields are themselves algorithm values (usually a digest name such as to_algorithm of SHA_256).
Recover typed params from a JS AlgorithmIdentifier by dispatching on its name. Key-generation and import parameter objects are told apart from same-named operation parameters by which fields are present (e.g. modulusLength vs hash for RSA, namedCurve for EC, iv/counter vs length for AES). An unrecognised name maps to Other.
class type subtleCrypto = object ... endclass type crypto = object ... endval subtle : unit -> subtleCrypto Js.t(crypto ())##.subtle. Only available in a secure context.