Skip to content

Commit

Permalink
0.7 compat fixes (#51)
Browse files Browse the repository at this point in the history
* Explicitly convert to `Vector()` for 0.7 compatibility

* Quash 0.7 errors

* Add Compat dependency and `global nerrors` everywhere

* Add `codeunits(x) = x` shim for Julia 0.6.x
  • Loading branch information
staticfloat authored Jan 14, 2018
1 parent 939e8f5 commit 5ca2ad1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
julia 0.6
Compat
10 changes: 8 additions & 2 deletions src/SHA.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__precompile__()

module SHA
using Compat

# Export convenience functions, context types, update!() and digest!() functions
export sha1, SHA1_CTX, update!, digest!
Expand All @@ -25,6 +26,11 @@ include("sha3.jl")
include("common.jl")
include("hmac.jl")

# Compat.jl-like shim for codeunits() on Julia <= 0.6:
if VERSION < v"0.7.0-DEV.3213"
codeunits(x) = x
end

# Create data types and convenience functions for each hash implemented
for (f, ctx) in [(:sha1, :SHA1_CTX),
(:sha224, :SHA224_CTX),
Expand Down Expand Up @@ -55,7 +61,7 @@ for (f, ctx) in [(:sha1, :SHA1_CTX),
end

# AbstractStrings are a pretty handy thing to be able to crunch through
$f(str::AbstractString) = $f(Vector{UInt8}(str))
$f(str::AbstractString) = $f(Vector{UInt8}(codeunits(str)))
$g(key::Vector{UInt8}, str::AbstractString) = $g(key, Vector{UInt8}(str))

# Convenience function for IO devices, allows for things like:
Expand All @@ -64,7 +70,7 @@ for (f, ctx) in [(:sha1, :SHA1_CTX),
# done
function $f(io::IO, chunk_size=4*1024)
ctx = $ctx()
buff = Vector{UInt8}(chunk_size)
buff = Vector{UInt8}(uninitialized, chunk_size)
while !eof(io)
num_read = readbytes!(io, buff)
update!(ctx, buff[1:num_read])
Expand Down
2 changes: 1 addition & 1 deletion src/sha3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function transform!(context::T) where {T<:SHA3_CTX}
for idx in 1:div(blocklen(T),8)
context.state[idx] = context.state[idx] unsafe_load(pbuf, idx)
end
bc = Vector{UInt64}(5)
bc = Vector{UInt64}(uninitialized, 5)

# We always assume 24 rounds
for round in 0:23
Expand Down
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const SHA384_CTX = SHA2_384_CTX
const SHA512_CTX = SHA2_512_CTX

# SHA1 is special; he needs extra workspace
SHA1_CTX() = SHA1_CTX(copy(SHA1_initial_hash_value), 0, zeros(UInt8, blocklen(SHA1_CTX)), Vector{UInt32}(80))
SHA1_CTX() = SHA1_CTX(copy(SHA1_initial_hash_value), 0, zeros(UInt8, blocklen(SHA1_CTX)), Vector{UInt32}(uninitialized, 80))


# Copy functions
Expand Down
13 changes: 11 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SHA
using Compat

# Define some data we will run our tests on
lorem = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
Expand Down Expand Up @@ -101,7 +102,7 @@ sha3_512 => [
]
)

function describe_hash{S<:SHA.SHA_CTX}(T::Type{S})
function describe_hash(T::Type{S}) where {S <: SHA.SHA_CTX}
if T <: SHA.SHA1_CTX return "SHA1" end
if T <: SHA.SHA2_CTX return "SHA2-$(SHA.digestlen(T)*8)" end
if T <: SHA.SHA3_CTX return "SHA3-$(SHA.digestlen(T)*8)" end
Expand All @@ -112,6 +113,8 @@ println("Loaded hash types: $(join(sort([describe_hash(t[2]) for t in sha_types]
# First, test processing the data in one go
nerrors = 0
for idx in 1:length(data)
global nerrors

desc = data_desc[idx]
print("Testing on $desc$(join(["." for z in 1:(34-length(desc))]))")
nerrors_old = nerrors
Expand Down Expand Up @@ -149,6 +152,8 @@ end
print("Testing on one million a's (chunked properly)")
nerrors_old = nerrors
for sha_idx in 1:length(sha_funcs)
global nerrors

ctx = sha_types[sha_funcs[sha_idx]]()
SHA.update!(ctx, so_many_as_array[1:2*SHA.blocklen(typeof(ctx))])
SHA.update!(ctx, so_many_as_array[2*SHA.blocklen(typeof(ctx))+1:end])
Expand All @@ -175,6 +180,7 @@ println("Done! [$(nerrors - nerrors_old) errors]")
print("Testing on one million a's (chunked clumsily)")
nerrors_old = nerrors
for sha_idx in 1:length(sha_funcs)
global nerrors
ctx = sha_types[sha_funcs[sha_idx]]()

# Get indices awkwardly placed for the blocklength of this hash type
Expand Down Expand Up @@ -218,7 +224,8 @@ for (key, msg, fun, hash) in (
(b"key", b"The quick brown fox jumps over the lazy dog", hmac_sha1, "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"),
(b"key", b"The quick brown fox jumps over the lazy dog", hmac_sha256, "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"),
)
digest = bytes2hex(fun(key, msg))
global nerrors
digest = bytes2hex(fun(Vector(key), Vector(msg)))
if digest != hash
print("\n")
warn(
Expand All @@ -242,6 +249,7 @@ else
end

for idx in 1:length(ctxs)
global nerrors
# Part #1: copy
print("Testing copy function @ $(ctxs[idx]) ...")
try
Expand All @@ -265,6 +273,7 @@ end

# test error if eltype of input is not UInt8
for f in sha_funcs
global nerrors
try
f(UInt32[0x23467, 0x324775])
warn("Non-UInt8 Arrays should fail")
Expand Down

0 comments on commit 5ca2ad1

Please sign in to comment.