moonpool/ocaml/Stdlib/Digest/index.html
2025-09-26 19:08:06 +00:00

2 lines
12 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Digest (ocaml.Stdlib.Digest)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 3.1.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> <a href="../../../index.html">Index</a> &#x00BB; <a href="../../index.html">ocaml</a> &#x00BB; <a href="../index.html">Stdlib</a> &#x00BB; Digest</nav><header class="odoc-preamble"><h1>Module <code><span>Stdlib.Digest</span></code></h1><p>Message digest.</p><p>This module provides functions to compute 'digests', also known as 'hashes', of arbitrary-length strings or files. The supported hashing algorithms are BLAKE2 and MD5.</p></header><div class="odoc-tocs"><nav class="odoc-toc odoc-local-toc"><ul><li><a href="#basic-functions">Basic functions</a></li><li><a href="#generic-interface">Generic interface</a></li><li><a href="#specific-hash-functions">Specific hash functions</a></li></ul></nav></div><div class="odoc-content"><h2 id="basic-functions"><a href="#basic-functions" class="anchor"></a>Basic functions</h2><p>The functions in this section use the MD5 hash function to produce 128-bit digests (16 bytes). MD5 is not cryptographically secure. Hence, these functions should not be used for security-sensitive applications. The BLAKE2 functions below are cryptographically secure.</p><div class="odoc-spec"><div class="spec type anchored" id="type-t"><a href="#type-t" class="anchor"></a><code><span><span class="keyword">type</span> t</span><span> = string</span></code></div><div class="spec-doc"><p>The type of digests: 16-byte strings.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-compare"><a href="#val-compare" class="anchor"></a><code><span><span class="keyword">val</span> compare : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> int</span></code></div><div class="spec-doc"><p>The comparison function for 16-byte digests, with the same specification as <a href="../index.html#val-compare"><code>Stdlib.compare</code></a> and the implementation shared with <a href="../String/index.html#val-compare"><code>String.compare</code></a>. Along with the type <code>t</code>, this function <code>compare</code> allows the module <code>Digest</code> to be passed as argument to the functors <a href="../Set/Make/index.html"><code>Set.Make</code></a> and <a href="../Map/Make/index.html"><code>Map.Make</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.00</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-equal"><a href="#val-equal" class="anchor"></a><code><span><span class="keyword">val</span> equal : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> bool</span></code></div><div class="spec-doc"><p>The equal function for 16-byte digests.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.03</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-string"><a href="#val-string" class="anchor"></a><code><span><span class="keyword">val</span> string : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return the digest of the given string.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-bytes"><a href="#val-bytes" class="anchor"></a><code><span><span class="keyword">val</span> bytes : <span>bytes <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return the digest of the given byte sequence.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.02</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-substring"><a href="#val-substring" class="anchor"></a><code><span><span class="keyword">val</span> substring : <span>string <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>Digest.substring s ofs len</code> returns the digest of the substring of <code>s</code> starting at index <code>ofs</code> and containing <code>len</code> characters.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-subbytes"><a href="#val-subbytes" class="anchor"></a><code><span><span class="keyword">val</span> subbytes : <span>bytes <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p><code>Digest.subbytes s ofs len</code> returns the digest of the subsequence of <code>s</code> starting at index <code>ofs</code> and containing <code>len</code> bytes.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.02</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-channel"><a href="#val-channel" class="anchor"></a><code><span><span class="keyword">val</span> channel : <span><a href="../index.html#type-in_channel">in_channel</a> <span class="arrow">&#45;&gt;</span></span> <span>int <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>If <code>len</code> is nonnegative, <code>Digest.channel ic len</code> reads <code>len</code> characters from channel <code>ic</code> and returns their digest, or raises <code>End_of_file</code> if end-of-file is reached before <code>len</code> characters are read. If <code>len</code> is negative, <code>Digest.channel ic len</code> reads all characters from <code>ic</code> until end-of-file is reached and return their digest.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-file"><a href="#val-file" class="anchor"></a><code><span><span class="keyword">val</span> file : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Return the digest of the file whose name is given.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-output"><a href="#val-output" class="anchor"></a><code><span><span class="keyword">val</span> output : <span><a href="../index.html#type-out_channel">out_channel</a> <span class="arrow">&#45;&gt;</span></span> <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> unit</span></code></div><div class="spec-doc"><p>Write a digest on the given output channel.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-input"><a href="#val-input" class="anchor"></a><code><span><span class="keyword">val</span> input : <span><a href="../index.html#type-in_channel">in_channel</a> <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Read a digest from the given input channel.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-to_hex"><a href="#val-to_hex" class="anchor"></a><code><span><span class="keyword">val</span> to_hex : <span><a href="#type-t">t</a> <span class="arrow">&#45;&gt;</span></span> string</span></code></div><div class="spec-doc"><p>Return the printable hexadecimal representation of the given digest.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <a href="../index.html#exception-Invalid_argument"><code>Invalid_argument</code></a> <p>if the argument is not exactly 16 bytes.</p></li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-of_hex"><a href="#val-of_hex" class="anchor"></a><code><span><span class="keyword">val</span> of_hex : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Convert a hexadecimal representation back into the corresponding digest.</p><ul class="at-tags"><li class="raises"><span class="at-tag">raises</span> <a href="../index.html#exception-Invalid_argument"><code>Invalid_argument</code></a> <p>if the argument is not exactly 32 hexadecimal characters.</p></li></ul><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 5.2</li></ul></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-from_hex"><a href="#val-from_hex" class="anchor"></a><code><span><span class="keyword">val</span> from_hex : <span>string <span class="arrow">&#45;&gt;</span></span> <a href="#type-t">t</a></span></code></div><div class="spec-doc"><p>Same function as <a href="#val-of_hex"><code>Digest.of_hex</code></a>.</p><ul class="at-tags"><li class="since"><span class="at-tag">since</span> 4.00</li></ul></div></div><h2 id="generic-interface"><a href="#generic-interface" class="anchor"></a>Generic interface</h2><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-S"><a href="#module-type-S" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S/index.html">S</a></span><span> = <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div><div class="spec-doc"><p>The signature for a hash function that produces digests of length <code>hash_length</code> from character strings, byte arrays, and files.</p></div></div><h2 id="specific-hash-functions"><a href="#specific-hash-functions" class="anchor"></a>Specific hash functions</h2><div class="odoc-spec"><div class="spec module anchored" id="module-BLAKE128"><a href="#module-BLAKE128" class="anchor"></a><code><span><span class="keyword">module</span> <a href="BLAKE128/index.html">BLAKE128</a></span><span> : <a href="module-type-S/index.html">S</a></span></code></div><div class="spec-doc"><p><code>BLAKE128</code> is the BLAKE2b hash function producing 128-bit (16-byte) digests. It is cryptographically secure. However, the small size of the digests enables brute-force attacks in <code>2{^64}</code> attempts.</p></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-BLAKE256"><a href="#module-BLAKE256" class="anchor"></a><code><span><span class="keyword">module</span> <a href="BLAKE256/index.html">BLAKE256</a></span><span> : <a href="module-type-S/index.html">S</a></span></code></div><div class="spec-doc"><p><code>BLAKE256</code> is the BLAKE2b hash function producing 256-bit (32-byte) digests. It is cryptographically secure, and the digests are large enough to thwart brute-force attacks.</p></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-BLAKE512"><a href="#module-BLAKE512" class="anchor"></a><code><span><span class="keyword">module</span> <a href="BLAKE512/index.html">BLAKE512</a></span><span> : <a href="module-type-S/index.html">S</a></span></code></div><div class="spec-doc"><p><code>BLAKE512</code> is the BLAKE2b hash function producing 512-bit (64-byte) digests. It is cryptographically secure, and the digests are large enough to thwart brute-force attacks.</p></div></div><div class="odoc-spec"><div class="spec module anchored" id="module-MD5"><a href="#module-MD5" class="anchor"></a><code><span><span class="keyword">module</span> <a href="MD5/index.html">MD5</a></span><span> : <a href="module-type-S/index.html">S</a></span></code></div><div class="spec-doc"><p><code>MD5</code> is the MD5 hash function. It produces 128-bit (16-byte) digests and is not cryptographically secure at all. It should be used only for compatibility with earlier designs that mandate the use of MD5.</p></div></div></div></body></html>