Module: BSV::Primitives::Digest
- Defined in:
- lib/bsv/primitives/digest.rb
Overview
Cryptographic hash functions and HMAC operations.
Thin wrappers around OpenSSL::Digest and OpenSSL::HMAC providing the hash algorithms used throughout the BSV protocol: SHA-1, SHA-256, double-SHA-256, SHA-512, RIPEMD-160, Hash160, HMAC, and PBKDF2.
Class Method Summary collapse
-
.hash160(data) ⇒ String
Compute Hash160: RIPEMD-160(SHA-256(data)).
-
.hmac_sha256(key, data) ⇒ String
Compute HMAC-SHA-256.
-
.hmac_sha512(key, data) ⇒ String
Compute HMAC-SHA-512.
-
.pbkdf2_hmac_sha512(password, salt, iterations: 2048, key_length: 64) ⇒ String
Derive a key using PBKDF2-HMAC-SHA-512.
-
.ripemd160(data) ⇒ String
Compute RIPEMD-160 digest.
-
.sha1(data) ⇒ String
Compute SHA-1 digest.
-
.sha256(data) ⇒ String
Compute SHA-256 digest.
-
.sha256d(data) ⇒ String
(also: hash256)
Compute double-SHA-256 (SHA-256d) digest.
-
.sha512(data) ⇒ String
Compute SHA-512 digest.
Class Method Details
.hash160(data) ⇒ String
Compute Hash160: RIPEMD-160(SHA-256(data)).
Standard Bitcoin hash used for addresses and P2PKH script matching.
66 67 68 |
# File 'lib/bsv/primitives/digest.rb', line 66 def hash160(data) ripemd160(sha256(data)) end |
.hmac_sha256(key, data) ⇒ String
Compute HMAC-SHA-256.
75 76 77 |
# File 'lib/bsv/primitives/digest.rb', line 75 def hmac_sha256(key, data) OpenSSL::HMAC.digest('SHA256', key, data) end |
.hmac_sha512(key, data) ⇒ String
Compute HMAC-SHA-512.
84 85 86 |
# File 'lib/bsv/primitives/digest.rb', line 84 def hmac_sha512(key, data) OpenSSL::HMAC.digest('SHA512', key, data) end |
.pbkdf2_hmac_sha512(password, salt, iterations: 2048, key_length: 64) ⇒ String
Derive a key using PBKDF2-HMAC-SHA-512.
Used by BIP-39 to convert mnemonic phrases into seeds.
97 98 99 |
# File 'lib/bsv/primitives/digest.rb', line 97 def pbkdf2_hmac_sha512(password, salt, iterations: 2048, key_length: 64) OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, 'sha512') end |
.ripemd160(data) ⇒ String
Compute RIPEMD-160 digest.
56 57 58 |
# File 'lib/bsv/primitives/digest.rb', line 56 def ripemd160(data) Ripemd160.digest(data) end |
.sha1(data) ⇒ String
Compute SHA-1 digest.
19 20 21 |
# File 'lib/bsv/primitives/digest.rb', line 19 def sha1(data) OpenSSL::Digest::SHA1.digest(data) end |
.sha256(data) ⇒ String
Compute SHA-256 digest.
27 28 29 |
# File 'lib/bsv/primitives/digest.rb', line 27 def sha256(data) OpenSSL::Digest::SHA256.digest(data) end |
.sha256d(data) ⇒ String Also known as: hash256
Compute double-SHA-256 (SHA-256d) digest.
Used extensively in Bitcoin for transaction and block hashing.
37 38 39 |
# File 'lib/bsv/primitives/digest.rb', line 37 def sha256d(data) sha256(sha256(data)) end |
.sha512(data) ⇒ String
Compute SHA-512 digest.
48 49 50 |
# File 'lib/bsv/primitives/digest.rb', line 48 def sha512(data) OpenSSL::Digest::SHA512.digest(data) end |