-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SHA objects #96
Comments
See |
A quick Proof-Of-Concept impl: If we really need this, I'd like to add it to the base and have SHA, MD5, CRC32, GitHash... all reuse these codes. hash_obj.jl # SPDX-License-Identifier: MIT
abstract type AbstractHash end
"""
HashBytes{N}
A hash object identifier. It is a `N` byte string.
"""
struct HashBytes{N} <: AbstractHash where {N}
val::NTuple{N, UInt8}
HashBytes(val::NTuple{N, UInt8}) where N = new{N}(val)
end
HashBytes{N}() where N = HashBytes(ntuple(i->zero(UInt8), N))
HashBytes(h::HashBytes) = h
function HashBytes{N}(u8::Vector{UInt8}) where N
@assert N == length(u8) "Hash length not match"
HashBytes(ntuple(idx->u8[idx], N))
end
HashBytes(s::AbstractString) = error("not impl")
import Base.show
function show(io::IO, hash_bytes::HashBytes{N}) where N
hash = join( repr(u)[3:end] for u in hash_bytes.val )
print(io, "HashBytes{$N}($(repr(hash)))")
end
# ==== Generate Hash Type Definitions for All SHA Types
# Examples:
# const Sha1Hash = HashBytes{20}
# const Sha3_512Hash = HashBytes{64}
using SHA
for (sha_prefix, sha_type) in [(:Sha1, :SHA1_CTX),
(:Sha224, :SHA224_CTX),
(:Sha256, :SHA256_CTX),
(:Sha384, :SHA384_CTX),
(:Sha512, :SHA512_CTX),
(:Sha2_224, :SHA2_224_CTX),
(:Sha2_256, :SHA2_256_CTX),
(:Sha2_384, :SHA2_384_CTX),
(:Sha2_512, :SHA2_512_CTX),
(:Sha3_224, :SHA3_224_CTX),
(:Sha3_256, :SHA3_256_CTX),
(:Sha3_384, :SHA3_384_CTX),
(:Sha3_512, :SHA3_512_CTX)]
hashsha_type = Symbol(sha_prefix, :Hash)
@eval begin
hashtype_len = SHA.digestlen($sha_type)
const $(hashsha_type) = HashBytes{hashtype_len}
end
end
# ---- examples:
Sha1Hash(sha1(""))
Sha3_256Hash(sha3_256(""))
Sha3_512Hash(sha3_512("")) example outout: julia> Sha1Hash(sha1(""))
HashBytes{20}("da39a3ee5e6b4b0d3255bfef95601890afd80709")
julia> Sha3_256Hash(sha3_256(""))
HashBytes{32}("a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a")
julia> Sha3_512Hash(sha3_512(""))
HashBytes{64}("a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26") |
Can you explain a bit more about what you want and why it would be useful? I have heard strong arguments both for hashes being objects, and for hash contexts being objects, but the hashes themselves being just arrays of bytes. I'd like to hear your argument for why it's better that they are their own objects. If its just for dispatch, I think a higher-level package like AbstractHashing or something similar may be a better fit for these kinds of concerns. I myself wanted something that lives higher level than SHA.jl (and can work with MD5 and whatnot) so I wrote this mini package to make dealing with different hashes easier. You can then constrain things to only take a certain hash type via snippets like this. |
I think the main advantages are:
|
Yes, so my main point would be that we probably want an Bottom-up; define Top-down; create a I think the bottom-up organization is better, but I don't think we want |
Possibly? This is complicated somewhat by the fact that What if we add |
The downside to this is that it's then only available in Julia v1.12+, and if we want to change something about how hash functions work, we have to wait for a new Julia version. I think it's actually better to have an |
It would be helpful if for each hash there was an object representing a hash (e.g.
SHA1
,SHA256
etc), similar toUUID
in Base.@staticfloat
The text was updated successfully, but these errors were encountered: