Skip to content
FutureBlitz

    Install

    Import std.crypto.hashes

    std.crypto.hashes

    The MD5, SHA-1, SHA-256, and SHA-512 hashes.

    Md5, Sha1, Sha256, and Sha512, over mbedTLS: each one's Hash gives the hash of bytes, or of a String's bytes, at once, and an object of it adds them in turn. MD5 and SHA-1 are broken, since two inputs with one hash can be made, so they're only for checksums and the formats that ask for them; Sha256 and Sha512 are for the rest.

    std/crypto/hashes.fb

    Types

    Hasher interface

    Public Interface Hasher

    What hashes bytes added to it in turn. Finish gives the hash of what was added, and starts again, empty.

    Examples

    Function HashOf(hasher: Hasher, parts: String[]): UInt8[]
        For part In parts
            hasher.Add(part)
        End For
        Return hasher.Finish()
    End Function
    
    Expect(HashOf(Sha256(), ["a", "b", "c"]) == Sha256.Hash("abc"))

    std/crypto/hashes.fb, line 41

    • Method Add(data: UInt8[]) method #

      Adds data to what it hashes.

    • Method Add(text: String) method #

      Adds text's UTF-8 bytes to what it hashes.

    • Method Finish(): UInt8[] method #

      The hash of what was added, which starts it again, empty.

    Md5 class

    Public Class Md5 Implements Hasher

    MD5's 16 bytes, only for checksums.

    Examples

    Expect(Hex.Encode(Md5.Hash("")) == "d41d8cd98f00b204e9800998ecf8427e")

    std/crypto/hashes.fb, line 58

    • Public New() constructor #

      A new Md5, of nothing.

    • Public Static Method Hash(data: UInt8[]): UInt8[] static method #

      The MD5 of data.

    • Public Static Method Hash(text: String): UInt8[] static method #

      The MD5 of text's UTF-8 bytes.

    • Public Method Add(data: UInt8[]) method #

      Adds data to what it hashes.

    • Public Method Add(text: String) method #

      Adds text's UTF-8 bytes to what it hashes.

    • Public Method Finish(): UInt8[] method #

      The 16-byte MD5 of what was added, after which it starts again, empty.

    Sha1 class

    Public Class Sha1 Implements Hasher

    SHA-1's 20 bytes, only for checksums and the formats that ask for it, like Git's.

    Examples

    Expect(Hex.Encode(Sha1.Hash("abc")) == "a9993e364706816aba3e25717850c26c9cd0d89d")

    std/crypto/hashes.fb, line 97

    • Public New() constructor #

      A new Sha1, of nothing.

    • Public Static Method Hash(data: UInt8[]): UInt8[] static method #

      The SHA-1 of data.

    • Public Static Method Hash(text: String): UInt8[] static method #

      The SHA-1 of text's UTF-8 bytes.

    • Public Method Add(data: UInt8[]) method #

      Adds data to what it hashes.

    • Public Method Add(text: String) method #

      Adds text's UTF-8 bytes to what it hashes.

    • Public Method Finish(): UInt8[] method #

      The 20-byte SHA-1 of what was added, after which it starts again, empty.

    Sha256 class

    Public Class Sha256 Implements Hasher

    SHA-256's 32 bytes.

    Examples

    Expect(Hex.Encode(Sha256.Hash("abc")) ==
           "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
    Local hasher = Sha256()
    hasher.Add("ab")
    hasher.Add("c".Bytes)
    Expect(hasher.Finish() == Sha256.Hash("abc"))

    std/crypto/hashes.fb, line 141

    • Public New() constructor #

      A new Sha256, of nothing.

    • Public Static Method Hash(data: UInt8[]): UInt8[] static method #

      The SHA-256 of data.

    • Public Static Method Hash(text: String): UInt8[] static method #

      The SHA-256 of text's UTF-8 bytes.

    • Public Method Add(data: UInt8[]) method #

      Adds data to what it hashes.

    • Public Method Add(text: String) method #

      Adds text's UTF-8 bytes to what it hashes.

    • Public Method Finish(): UInt8[] method #

      The 32-byte SHA-256 of what was added, after which it starts again, empty.

    Sha512 class

    Public Class Sha512 Implements Hasher

    SHA-512's 64 bytes.

    Examples

    Expect(Sha512.Hash("abc").Count == 64)

    std/crypto/hashes.fb, line 179

    • Public New() constructor #

      A new Sha512, of nothing.

    • Public Static Method Hash(data: UInt8[]): UInt8[] static method #

      The SHA-512 of data.

    • Public Static Method Hash(text: String): UInt8[] static method #

      The SHA-512 of text's UTF-8 bytes.

    • Public Method Add(data: UInt8[]) method #

      Adds data to what it hashes.

    • Public Method Add(text: String) method #

      Adds text's UTF-8 bytes to what it hashes.

    • Public Method Finish(): UInt8[] method #

      The 64-byte SHA-512 of what was added, after which it starts again, empty.

    Report an issue with this page or with FutureBlitz on GitHub.