Skip to content
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

Use Vector instead of Array to simplify types. #97

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ julia> bytes2hex(sha256("test"))
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
```

Each exported function (at the time of this writing, SHA-1, SHA-2 224, 256, 384 and 512, and SHA-3 224, 256, 384 and 512 functions are implemented) takes in either an `Array{UInt8}`, a `ByteString` or an `IO` object. This makes it trivial to checksum a file:
Each exported function (at the time of this writing, SHA-1, SHA-2 224, 256, 384 and 512, and SHA-3 224, 256, 384 and 512 functions are implemented) takes in either an `Vector{UInt8}`, a `ByteString` or an `IO` object. This makes it trivial to checksum a file:

```julia
shell> cat /tmp/test.txt
Expand All @@ -29,7 +29,7 @@ julia> using SHA
julia> open("/tmp/test.txt") do f
sha2_256(f)
end
32-element Array{UInt8,1}:
32-element Vector{UInt8}:
0x9f
0x86
0xd0
Expand All @@ -52,6 +52,6 @@ Note the lack of a newline at the end of `/tmp/text.txt`. Julia automatically i

Due to the colloquial usage of `sha256` to refer to `sha2_256`, convenience functions are provided, mapping `shaxxx()` function calls to `sha2_xxx()`. For SHA-3, no such colloquialisms exist and the user must use the full `sha3_xxx()` names.

`shaxxx()` takes `AbstractString` and array-like objects (`NTuple` and `Array`) with elements of type `UInt8`.
`shaxxx()` takes `AbstractString` and array-like objects (`NTuple` and `Vector`) with elements of type `UInt8`.

Note that, at the time of this writing, the SHA3 code is not optimized, and as such is roughly an order of magnitude slower than SHA2. Pull requests are welcome.
4 changes: 2 additions & 2 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ julia> using SHA
julia> open("/tmp/test.txt") do f
sha2_256(f)
end
32-element Array{UInt8,1}:
32-element Vector{UInt8}:
0x9f
0x86
0xd0
Expand All @@ -55,7 +55,7 @@ julia> open("/tmp/test.txt") do f
Due to the colloquial usage of `sha256` to refer to `sha2_256`, convenience functions are provided, mapping `shaxxx()` function calls to `sha2_xxx()`.
For SHA-3, no such colloquialisms exist and the user must use the full `sha3_xxx()` names.

`shaxxx()` takes `AbstractString` and array-like objects (`NTuple` and `Array`) with elements of type `UInt8`.
`shaxxx()` takes `AbstractString` and array-like objects (`NTuple` and `Vector`) with elements of type `UInt8`.

**SHA-1**
```@docs
Expand Down
4 changes: 2 additions & 2 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ end
"""
digest!(context)

Finalize the SHA context and return the hash as array of bytes (Array{Uint8, 1}).
Finalize the SHA context and return the hash as array of bytes (Vector{Uint8}).
Updating the context after calling `digest!` on it will error.

# Examples
Expand All @@ -93,7 +93,7 @@ SHA1 hash state
julia> update!(ctx, b"data to to be hashed")

julia> digest!(ctx)
20-element Array{UInt8,1}:
20-element Vector{UInt8}:
0x83
0xe4
Expand Down
16 changes: 8 additions & 8 deletions src/shake.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
abstract type SHAKE <: SHA3_CTX end
# note, that field property used has differend uses, depending on T<:SHAKE or T<:SHA3_CTX
mutable struct SHAKE_128_CTX <: SHAKE
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end
mutable struct SHAKE_256_CTX <: SHAKE
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end

Expand Down Expand Up @@ -111,7 +111,7 @@ Hash data using the `shake128` algorithm and return the first d resulting bytes.
function shake128(data::AbstractBytes,d::UInt)
ctx = SHAKE_128_CTX()
update!(ctx, data)
M = Array{UInt8,1}(undef,d) # prealloc
M = Vector{UInt8}(undef,d) # prealloc
p = pointer(M)
digest!(ctx,d,p)
return M
Expand All @@ -124,7 +124,7 @@ Hash data using the `shake258` algorithm and return the first d resulting bytes.
function shake256(data::AbstractBytes,d::UInt)
ctx = SHAKE_256_CTX()
update!(ctx, data)
M = Array{UInt8,1}(undef,d) # prealloc
M = Vector{UInt8}(undef,d) # prealloc
p = pointer(M)
digest!(ctx,d,p)
return M
Expand Down
46 changes: 23 additions & 23 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ import Base: copy
# We derive SHA1_CTX straight from SHA_CTX since it doesn't have a
# family of types like SHA2 or SHA3 do
mutable struct SHA1_CTX <: SHA_CTX
state::Array{UInt32,1}
state::Vector{UInt32}
bytecount::UInt64
buffer::Array{UInt8,1}
W::Array{UInt32,1}
buffer::Vector{UInt8}
W::Vector{UInt32}
used::Bool
end

# SHA2 224/256/384/512-bit Context Structures
mutable struct SHA2_224_CTX <: SHA2_CTX
state::Array{UInt32,1}
state::Vector{UInt32}
bytecount::UInt64
buffer::Array{UInt8,1}
buffer::Vector{UInt8}
used::Bool
end

mutable struct SHA2_256_CTX <: SHA2_CTX
state::Array{UInt32,1}
state::Vector{UInt32}
bytecount::UInt64
buffer::Array{UInt8,1}
buffer::Vector{UInt8}
used::Bool
end

mutable struct SHA2_384_CTX <: SHA2_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
buffer::Vector{UInt8}
used::Bool
end

mutable struct SHA2_512_CTX <: SHA2_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
buffer::Vector{UInt8}
used::Bool
end

Expand Down Expand Up @@ -68,31 +68,31 @@ const SHA512_CTX = SHA2_512_CTX

# SHA3 224/256/384/512-bit context structures
mutable struct SHA3_224_CTX <: SHA3_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end
mutable struct SHA3_256_CTX <: SHA3_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end
mutable struct SHA3_384_CTX <: SHA3_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end
mutable struct SHA3_512_CTX <: SHA3_CTX
state::Array{UInt64,1}
state::Vector{UInt64}
bytecount::UInt128
buffer::Array{UInt8,1}
bc::Array{UInt64,1}
buffer::Vector{UInt8}
bc::Vector{UInt64}
used::Bool
end

Expand Down
Loading