module Secp256k1::Hash

Overview

The Secp256k1::Hash module wraps various hashing functions for convenience and exposes them for general use.

Defined in:

hash.cr

Constant Summary

BASE_56 = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz"

The Base-56 alphabet for Bitcoin mini-private keys is a Base-58 alphabet without 1 and o to additionally omit more similar-looking letters.

BASE_58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

The Base-58 alphabet for Bitcoin addresses is a Base-64 alphabet without 0, O, I, and l to omit similar-looking letters.

Class Method Summary

Class Method Detail

def self.base56_char(i : Int32) #

Gets a character from the Base-56 alphabet at position i.

Parameters:

  • i (Int32): the position in the Base-56 alphabet.
Secp256k1::Hash.base56_char 13
# => 'F'

[View source]
def self.base58_char(i : Int32) #

Gets a character from the Base-58 alphabet at position i.

Parameters:

  • i (Int32): the position in the Base-58 alphabet.
Secp256k1::Hash.base58_char 13
# => 'E'

[View source]
def self.base58_decode(s : String) #

Decodes a hexadecimal string from a Base-58 encoded string.

Parameters:

  • s (String): The Base-58 encoded string to be decoded.
Secp256k1::Hash.base58_decode "1CSSfnxKnQK1GDWSaWqNpYXSdfPTtSooHt"
# => "007d7935bde6c9341de87a4d64588783033e23472d7322c46b"

[View source]
def self.base58_encode(h : String) #

Encodes a Base-58 string from a hexadecimal string.

Parameters:

  • h (String): The hexadecimal string to be encoded.
Secp256k1::Hash.base58_encode "007d7935bde6c9341de87a4d64588783033e23472d7322c46b"
# => "1CSSfnxKnQK1GDWSaWqNpYXSdfPTtSooHt"

[View source]
def self.bin_to_hex(b : Bytes) #

Helper function to convert byte arrays to hexadecimal strings.

Parameters:

  • b (Bytes): the byte array to be converted.
Secp256k1::Hash.bin_to_hex Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]
=> "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"

[View source]
def self.hex_to_bin(h : String) #

Helper function to convert hexadecimal strings to byte arrays.

Parameters:

  • h (String): the hexadecimal string to be converted.
Secp256k1::Hash.hex_to_bin "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
=> Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]

[View source]
def self.keccak256(b : Bytes) #

Operating a Keccak-256 hash on a byte array.

Parameters:

  • b (Bytes): the byte array to be hashed.
Secp256k1::Hash.keccak256 Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]
# => "fcb41efa0456ba9f27e573422d6b5898c61da6f2137d07e4dae618eddd72e003"

[View source]
def self.keccak256_string(h : String) #

Operating a Keccak-256 hash on an actual string literal.

Parameters:

  • h (String): the string literal to be hashed.
Secp256k1::Hash.keccak256_string "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
# => "99cfa79866ec88f87f8e25a98a4b9873f3f8ee82482a317a5494572b00f51cec"

[View source]
def self.ripemd160(b : Bytes) #

Operating a RIPEMD-160 hash on a byte array.

Parameters:

  • b (Bytes): the byte array to be hashed.
Secp256k1::Hash.ripemd160 Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]
# => "5f3455f9ac58e25be08c99a7090108751b4796b9"

[View source]
def self.ripemd160_string(h : String) #

Operating a RIPEMD-160 hash on an actual string literal.

Parameters:

  • h (String): the string literal to be hashed.
Secp256k1::Hash.ripemd160_string "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
# => "46dff6cd5666c8e67db26ac0dfaf685bf71fc5f6"

[View source]
def self.sha256(b : Bytes) #

Operating a SHA2-256 hash on a byte array.

Parameters:

  • b (Bytes): the byte array to be hashed.
Secp256k1::Hash.sha256 Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]
# => "2739cc5f45c0e05236527e4e687dc54f0d5e88be64b9a90e5264a6721c0c71f2"

[View source]
def self.sha256_string(h : String) #

Operating a SHA2-256 hash on an actual string literal.

Parameters:

  • h (String): the string literal to be hashed.
Secp256k1::Hash.sha256_string "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
# => "452a41c28c9981faebb402095a5d553de28dc212338057aed27081110dfb907a"

[View source]
def self.sha3(b : Bytes) #

Operating a SHA3-256 hash on a byte array.

Parameters:

  • b (Bytes): the byte array to be hashed.
Secp256k1::Hash.sha3 Bytes[183, 149, 205, 44, 92, 224, 204, 99, 44, 161, 246, 94, 146, 27, 156, 117, 27, 54, 62, 151, 252, 174, 236, 129, 192, 42, 133, 183, 99, 68, 130, 104]
# => "66bb65180108362a3e25ba8282f7b96bfe840ce34a2e5dbc421aa8a590cc5f2e"

[View source]
def self.sha3_string(h : String) #

Operating a SHA3-256 hash on an actual string literal.

Parameters:

  • h (String): the string literal to be hashed.
Secp256k1::Hash.sha3_string "b795cd2c5ce0cc632ca1f65e921b9c751b363e97fcaeec81c02a85b763448268"
# => "aedc012933679615eb93fb0063f53010e6f0034e92aaccf97dacc46e338037e9"

[View source]