calculateHash is a method that computes a deterministic hash from selected object properties. Its design separates property selection, data assembly, and digest computation so the hashing algorithm can change independently. section 4.3.2 section 4.4.2
A composable implementation divides the method into these responsibilities:
- Selects the configured keys and extracts their values from the target object. section 4.4.2
- Assembles selected values into a predictable string representation, using JSON.stringify when values may be non-primitive. section 4.3.2 section 4.4.2
- Computes the digest through a replaceable cipher, first with a recursive implementation and later with Node.js crypto. section 4.3.2 section 4.4.2
- Composes these pure functions while keeping object context at the mixin boundary. section 4.3.2 section 4.4.2
Hashing a transaction
A transaction can combine its sender, recipient, and funds into a SHA-256 hexadecimal digest by applying HasHash(['sender', 'recipient', 'funds']). Repeated calls with unchanged selected properties produce the same hash. section 4.4.2
Listing 4.12 Final implementation of HasHash
const HasHash = (
keys,
options = {
algorithm: 'SHA256',
encoding: 'hex'
}
) => ({
calculateHash() {
return compose(
computeCipher(options),
assemble,
props(keys)
)(this); #1
}
});
The final HasHash implementation shows configurable keys and hash options flowing through props, assemble, and computeCipher. section 4.4.2
