Skip to content

Commit

Permalink
Merge pull request #39 from musm/tes
Browse files Browse the repository at this point in the history
update parameterized syntax
  • Loading branch information
staticfloat authored Jul 26, 2017
2 parents d487014 + 693c5c3 commit 90e1dff
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/base_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ sigma0_512(x) = (S64( 1, UInt64(x)) ⊻ S64( 8, UInt64(x)) ⊻ R( 7, UInt64(
sigma1_512(x) = (S64(19, UInt64(x)) S64(61, UInt64(x)) R( 6, UInt64(x)))

# Let's be able to bswap arrays of these types as well
bswap!{T<:Integer}(x::Vector{T}) = map!(bswap, x, x)
bswap!(x::Vector{<:Integer}) = map!(bswap, x, x)
4 changes: 2 additions & 2 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# update! takes in variable-length data, buffering it into blocklen()-sized pieces,
# calling transform!() when necessary to update the internal hash state.
function update!{T<:Union{SHA1_CTX,SHA2_CTX,SHA3_CTX}}(context::T, data::Array{UInt8,1})
function update!(context::T, data::Array{UInt8,1}) where {T<:Union{SHA1_CTX,SHA2_CTX,SHA3_CTX}}
# We need to do all our arithmetic in the proper bitwidth
UIntXXX = typeof(context.bytecount)

Expand Down Expand Up @@ -33,7 +33,7 @@ end


# Clear out any saved data in the buffer, append total bitlength, and return our precious hash!
function digest!{T<:Union{SHA1_CTX,SHA2_CTX}}(context::T)
function digest!(context::T) where {T<:Union{SHA1_CTX,SHA2_CTX}}
usedspace = context.bytecount % blocklen(T)
# If we have anything in the buffer still, pad and transform that data
if usedspace > 0
Expand Down
2 changes: 1 addition & 1 deletion src/sha2.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function transform!{T<:Union{SHA2_224_CTX,SHA2_256_CTX}}(context::T)
function transform!(context::T) where {T<:Union{SHA2_224_CTX,SHA2_256_CTX}}
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
# Initialize registers with the previous intermediate values (our state)
a = context.state[1]
Expand Down
4 changes: 2 additions & 2 deletions src/sha3.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function transform!{T<:SHA3_CTX}(context::T)
function transform!(context::T) where {T<:SHA3_CTX}
# First, update state with buffer
pbuf = Ptr{eltype(context.state)}(pointer(context.buffer))
for idx in 1:div(blocklen(T),8)
Expand Down Expand Up @@ -49,7 +49,7 @@ end


# Finalize data in the buffer, append total bitlength, and return our precious hash!
function digest!{T<:SHA3_CTX}(context::T)
function digest!(context::T) where {T<:SHA3_CTX}
usedspace = context.bytecount % blocklen(T)
# If we have anything in the buffer still, pad and transform that data
if usedspace < blocklen(T) - 1
Expand Down
14 changes: 7 additions & 7 deletions src/types.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Type hierarchy to aid in splitting up of SHA2 algorithms
# as SHA224/256 are similar, and SHA-384/512 are similar
@compat abstract type SHA_CTX end
@compat abstract type SHA2_CTX <: SHA_CTX end
@compat abstract type SHA3_CTX <: SHA_CTX end
abstract type SHA_CTX end
abstract type SHA2_CTX <: SHA_CTX end
abstract type SHA3_CTX <: SHA_CTX end
import Base: copy

# We derive SHA1_CTX straight from SHA_CTX since it doesn't have a
Expand Down Expand Up @@ -102,7 +102,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{T<:Union{SHA1_CTX,SHA2_CTX}}(::Type{T}) = blocklen(T) - 2*sizeof(state_type(T))
short_blocklen(::Type{T}) where {T<:Union{SHA1_CTX,SHA2_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 All @@ -126,9 +126,9 @@ SHA1_CTX() = SHA1_CTX(copy(SHA1_initial_hash_value), 0, zeros(UInt8, blocklen(SH


# Copy functions
copy{T <: SHA1_CTX}(ctx::T) = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer), copy(ctx.W))
copy{T <: SHA2_CTX}(ctx::T) = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer))
copy{T <: SHA3_CTX}(ctx::T) = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer))
copy(ctx::T) where {T<:SHA1_CTX} = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer), copy(ctx.W))
copy(ctx::T) where {T<:SHA2_CTX} = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer))
copy(ctx::T) where {T<:SHA3_CTX} = T(copy(ctx.state), ctx.bytecount, copy(ctx.buffer))


# Make printing these types a little friendlier
Expand Down

0 comments on commit 90e1dff

Please sign in to comment.