arrow-refined-types / arrow.refinement.digests / SHA256

SHA256

class SHA256

SHA256 constrains String to be a valid hexadecimal String of length 64

Safe nullable construction

import arrow.refinement.digests.SHA256

SHA256.orNull("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
// 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
SHA256.orNull("not-sha256")

Resolved constraints

SHA256.constraints("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
// [(true, Expected 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 to be a valid hexadecimal string), (true, Expected 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08 to have length 64)]
SHA256.constraints("not-sha256")
// [(false, Expected not-sha256 to be a valid hexadecimal string), (false, Expected not-sha256 to have length 64)]
SHA256.isValid("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
// true
SHA256.isValid("not-sha256")
// false

Folding validation

SHA256.fold("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", { "failed: $it" }, { "success: $it" })
// success: SHA256(value=9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08)
SHA256.fold("not-sha256", { "failed: $it" }, { "success: $it" })
// failed: [(false, Expected not-sha256 to be a valid hexadecimal string), (false, Expected not-sha256 to have length 64)]

Unsafe require

SHA256.require("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
// 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
try { SHA256.require("not-sha256") } catch (e: IllegalArgumentException) { e.message }
// Expected not-sha256 to be a valid hexadecimal string, Expected not-sha256 to have length 64

Types

Companion companion object Companion : Refined<String, SHA256>

Properties

value val value: String

Do you like Arrow?

Arrow Org
<