Skip to content

Commit 94c5d3f

Browse files
authored
Update some code to make it more julian (#114)
* Update cipher.jl Make the internals more julian. * Update hash_common.jl Update hash_common.jl to make it more julian. * Update hash.jl Make it more julian, dropping`AbstractString` in favor of `String`. * Update hmac.jl Make it more julian, dropping`AbstractString` in favor of `String`. * Update cipher.jl Fix indentation
1 parent 6393b99 commit 94c5d3f

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

src/cipher.jl

+16-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ end
2121

2222
# For much the same reasons as in hash_common.jl, we define a separate, more "Julia friendly" type
2323
struct CipherType
24-
name::AbstractString
24+
name::String
2525
context_size::Cuint
2626
block_size::Cuint
2727
key_size::Cuint
@@ -49,7 +49,7 @@ function CipherType(nc::NettleCipher)
4949
end
5050

5151
# The global dictionary of hash types we know how to construct
52-
const _cipher_types = Dict{AbstractString,CipherType}()
52+
const _cipher_types = Dict{String,CipherType}()
5353

5454
# We're going to load in each NettleCipher struct individually, deriving
5555
# HashAlgorithm types off of the names we find, and storing the output
@@ -83,20 +83,20 @@ function gen_key32_iv16(pw::Vector{UInt8}, salt::Vector{UInt8})
8383
end
8484

8585
function add_padding_PKCS5(data::Vector{UInt8}, block_size::Int)
86-
padlen = block_size - (sizeof(data) % block_size)
87-
return [data; map(i -> UInt8(padlen), 1:padlen)]
86+
padlen = block_size - (sizeof(data) % block_size)
87+
return [data; map(i -> UInt8(padlen), 1:padlen)]
8888
end
8989

9090
function trim_padding_PKCS5(data::Vector{UInt8})
91-
padlen = data[sizeof(data)]
92-
if all(data[end-padlen+1:end-1] .== data[end])
93-
return data[1:sizeof(data)-padlen]
94-
else
95-
throw(ArgumentError("Invalid PKCS5 padding"))
96-
end
91+
padlen = data[end]
92+
if all(data[end-padlen+1:end-1] .== data[end])
93+
return data[1:end-padlen]
94+
else
95+
throw(ArgumentError("Invalid PKCS5 padding"))
96+
end
9797
end
9898

99-
function Encryptor(name::AbstractString, key)
99+
function Encryptor(name::String, key)
100100
cipher_types = get_cipher_types()
101101
name = uppercase(name)
102102
if !haskey(cipher_types, name)
@@ -114,7 +114,7 @@ function Encryptor(name::AbstractString, key)
114114
return Encryptor(cipher_type, state)
115115
end
116116

117-
function Decryptor(name::AbstractString, key)
117+
function Decryptor(name::String, key)
118118
cipher_types = get_cipher_types()
119119
name = uppercase(name)
120120
if !haskey(cipher_types, name)
@@ -253,11 +253,11 @@ function encrypt(state::Encryptor, data)
253253
end
254254

255255
# The one-shot functions that make this whole thing so easy
256-
decrypt(name::AbstractString, key, data) = decrypt(Decryptor(name, key), data)
257-
encrypt(name::AbstractString, key, data) = encrypt(Encryptor(name, key), data)
256+
decrypt(name::String, key, data) = decrypt(Decryptor(name, key), data)
257+
encrypt(name::String, key, data) = encrypt(Encryptor(name, key), data)
258258

259-
decrypt(name::AbstractString, e::Symbol, iv::Vector{UInt8}, key, data) = decrypt(Decryptor(name, key), e, iv, data)
260-
encrypt(name::AbstractString, e::Symbol, iv::Vector{UInt8}, key, data) = encrypt(Encryptor(name, key), e, iv, data)
259+
decrypt(name::String, e::Symbol, iv::Vector{UInt8}, key, data) = decrypt(Decryptor(name, key), e, iv, data)
260+
encrypt(name::String, e::Symbol, iv::Vector{UInt8}, key, data) = encrypt(Encryptor(name, key), e, iv, data)
261261

262262
# Custom show overrides make this package have a little more pizzaz!
263263
function show(io::IO, x::CipherType)

src/hash.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Hasher
1010
end
1111

1212
# Constructor for Hasher
13-
function Hasher(name::AbstractString)
13+
function Hasher(name::String)
1414
hash_types = get_hash_types()
1515
name = uppercase(name)
1616
if !haskey(hash_types, name)
@@ -41,10 +41,10 @@ end
4141
hexdigest!(state::Hasher) = bytes2hex(digest!(state))
4242

4343
# The one-shot functions that makes this whole thing so easy.
44-
digest(hash_name::AbstractString, data) = digest!(update!(Hasher(hash_name), data))
45-
digest(hash_name::AbstractString, io::IO) = digest(hash_name, readall(io))
46-
hexdigest(hash_name::AbstractString, data) = hexdigest!(update!(Hasher(hash_name), data))
47-
hexdigest(hash_name::AbstractString, io::IO) = hexdigest(hash_name, readall(io))
44+
digest(hash_name::String, data) = digest!(update!(Hasher(hash_name), data))
45+
digest(hash_name::String, io::IO) = digest(hash_name, readall(io))
46+
hexdigest(hash_name::String, data) = hexdigest!(update!(Hasher(hash_name), data))
47+
hexdigest(hash_name::String, io::IO) = hexdigest(hash_name, readall(io))
4848

4949
# Custom show overrides make this package have a little more pizzaz!
5050
function show(io::IO, x::HashType)

src/hash_common.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function HashType(nh::NettleHash, nhptr::Ptr{Cvoid})
3737
end
3838

3939
# The global dictionary of hash types we know how to construct
40-
const _hash_types = Dict{AbstractString,HashType}()
40+
const _hash_types = Dict{String,HashType}()
4141

4242
# We're going to load in each NettleHash struct individually, deriving
4343
# HashAlgorithm types off of the names we find, and storing the output

src/hmac.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct HMACState
1313
end
1414

1515
# Constructor for HMACState
16-
function HMACState(name::AbstractString, key)
16+
function HMACState(name::String, key)
1717
hash_types = get_hash_types()
1818
name = uppercase(name)
1919
if !haskey(hash_types, name)
@@ -47,7 +47,7 @@ end
4747
hexdigest!(state::HMACState) = bytes2hex(digest!(state))
4848

4949
# The one-shot functions that makes this whole thing so easy
50-
digest(hmac_name::AbstractString, key, data) = digest!(update!(HMACState(hmac_name, key), data))
51-
hexdigest(hmac_name::AbstractString, key, data) = hexdigest!(update!(HMACState(hmac_name, key), data))
50+
digest(hmac_name::String, key, data) = digest!(update!(HMACState(hmac_name, key), data))
51+
hexdigest(hmac_name::String, key, data) = hexdigest!(update!(HMACState(hmac_name, key), data))
5252

5353
show(io::IO, x::HMACState) = write(io, "$(x.hash_type.name) HMAC state")

0 commit comments

Comments
 (0)