Skip to content

Commit

Permalink
Abstractions for MD5.jl (#49)
Browse files Browse the repository at this point in the history
* =Use abstract type, not union

* =Some abstractions
  • Loading branch information
oxinabox authored and staticfloat committed Jan 11, 2018
1 parent ad95112 commit 1d29970
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ function update!(context::T, data::U) where {T<:SHA_CTX,
end
end


# Clear out any saved data in the buffer, append total bitlength, and return our precious hash!
function digest!(context::T) where {T<:Union{SHA1_CTX,SHA2_CTX}}
# Pad the remainder leaving space for the bitcount
function pad_remainder!(context::T) where T<:SHA_CTX
usedspace = context.bytecount % blocklen(T)
# If we have anything in the buffer still, pad and transform that data
if usedspace > 0
Expand Down Expand Up @@ -64,7 +63,13 @@ function digest!(context::T) where {T<:Union{SHA1_CTX,SHA2_CTX}}
context.buffer[i] = 0x0
end
end
end


# Clear out any saved data in the buffer, append total bitlength, and return our precious hash!
# Note: SHA3_CTX has a more specialised method
function digest!(context::T) where T<:SHA_CTX
pad_remainder!(context)
# Store the length of the input data (in bits) at the end of the padding
bitcount_idx = div(short_blocklen(T), sizeof(context.bytecount)) + 1
pbuf = Ptr{typeof(context.bytecount)}(pointer(context.buffer))
Expand Down
2 changes: 1 addition & 1 deletion src/sha1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

function transform!(context::SHA1_CTX)
# Buffer is 16 elements long, we expand to 80
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
pbuf = buffer_pointer(context)
for i in 1:16
context.W[i] = bswap(unsafe_load(pbuf, i))
end
Expand Down
4 changes: 2 additions & 2 deletions src/sha2.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function transform!(context::T) where {T<:Union{SHA2_224_CTX,SHA2_256_CTX}}
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
pbuf = buffer_pointer(context)
# Initialize registers with the previous intermediate values (our state)
a = context.state[1]
b = context.state[2]
Expand Down Expand Up @@ -68,7 +68,7 @@ end


function transform!(context::Union{SHA2_384_CTX,SHA2_512_CTX})
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
pbuf = buffer_pointer(context)
# Initialize registers with the prev. intermediate value
a = context.state[1]
b = context.state[2]
Expand Down
7 changes: 6 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ state_type(::Type{SHA2_224_CTX}) = UInt32
state_type(::Type{SHA2_256_CTX}) = UInt32
state_type(::Type{SHA2_384_CTX}) = UInt64
state_type(::Type{SHA2_512_CTX}) = UInt64
state_type(::Type{SHA3_CTX}) = UInt64

# blocklen is the number of bytes of data processed by the transform!() function at once
blocklen(::Type{SHA1_CTX}) = UInt64(64)
Expand All @@ -102,7 +103,7 @@ blocklen(::Type{SHA3_512_CTX}) = UInt64(25*8 - 2*digestlen(SHA3_512_CTX))


# short_blocklen is the size of a block minus the width of bytecount
short_blocklen(::Type{T}) where {T<:Union{SHA1_CTX,SHA2_CTX}} = blocklen(T) - 2*sizeof(state_type(T))
short_blocklen(::Type{T}) where {T<:SHA_CTX} = blocklen(T) - 2*sizeof(state_type(T))

# Once the "blocklen" methods are defined, we can define our outer constructors for SHA types:
SHA2_224_CTX() = SHA2_224_CTX(copy(SHA2_224_initial_hash_value), 0, zeros(UInt8, blocklen(SHA2_224_CTX)))
Expand Down Expand Up @@ -142,3 +143,7 @@ show(io::IO, ::SHA3_224_CTX) = write(io, "SHA3 224-bit hash state")
show(io::IO, ::SHA3_256_CTX) = write(io, "SHA3 256-bit hash state")
show(io::IO, ::SHA3_384_CTX) = write(io, "SHA3 384-bit hash state")
show(io::IO, ::SHA3_512_CTX) = write(io, "SHA3 512-bit hash state")


# use out types to define a method to get a pointer to the state buffer
buffer_pointer(ctx::T) where {T<:SHA_CTX} = Ptr{state_type(T)}(pointer(ctx.buffer))

0 comments on commit 1d29970

Please sign in to comment.