模組 Digest

module Digest: sig .. end

訊息摘要。

此模組提供計算任意長度字串或檔案的「摘要」(digest),也稱為「雜湊」(hash)的函式。支援的雜湊演算法有 BLAKE2 和 MD5。


基本函式

本節中的函式使用 MD5 雜湊函式來產生 128 位元(16 位元組)的摘要。MD5 在密碼學上並不安全。因此,這些函式不應該用於對安全性敏感的應用程式。下方的 BLAKE2 函式在密碼學上是安全的。

type t = string 

摘要的類型:16 位元組字串。

val compare : t -> t -> int

用於 16 位元組摘要的比較函式,與 compare 具有相同的規範,且與 String.compare 共用實作。連同類型 t,此函式 compare 允許將模組 Digest 作為引數傳遞給函子 Set.MakeMap.Make

val equal : t -> t -> bool

用於 16 位元組摘要的相等函式。

val string : string -> t

回傳給定字串的摘要。

val bytes : bytes -> t

回傳給定位元組序列的摘要。

val substring : string -> int -> int -> t

Digest.substring s ofs len 回傳字串 s 中,從索引 ofs 開始,包含 len 個字元的子字串的摘要。

val subbytes : bytes -> int -> int -> t

Digest.subbytes s ofs len 回傳位元組序列 s 中,從索引 ofs 開始,包含 len 個位元組的子序列的摘要。

val channel : in_channel -> int -> t

如果 len 為非負數,Digest.channel ic len 會從通道 ic 讀取 len 個字元並回傳它們的摘要,或者如果讀取的字元數在達到 len 之前就到達檔案結尾,則會引發 End_of_file 異常。如果 len 為負數,Digest.channel ic len 會從 ic 讀取所有字元直到到達檔案結尾,並回傳它們的摘要。

val file : string -> t

回傳指定名稱檔案的摘要。

val output : out_channel -> t -> unit

將摘要寫入給定的輸出通道。

val input : in_channel -> t

從給定的輸入通道讀取摘要。

val to_hex : t -> string

回傳給定摘要的可列印十六進位表示法。

val of_hex : string -> t

將十六進位表示法轉換回對應的摘要。

val from_hex : string -> t

Digest.of_hex 相同的函式。

通用介面

module type S = sig .. end

從字元字串、位元組陣列和檔案產生長度為 hash_length 的摘要的雜湊函式簽章。

特定雜湊函式

module BLAKE128: S 

BLAKE128 是產生 128 位元 (16 位元組) 摘要的 BLAKE2b 雜湊函式。

module BLAKE256: S 

BLAKE256 是產生 256 位元 (32 位元組) 摘要的 BLAKE2b 雜湊函式。

module BLAKE512: S 

BLAKE512 是產生 512 位元 (64 位元組) 摘要的 BLAKE2b 雜湊函式。

module MD5: S 

MD5 是 MD5 雜湊函式。