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

encrypt method mentioned in the documentation does not actually work #263

Open
iuliadmtru opened this issue Jul 4, 2023 · 0 comments
Open

Comments

@iuliadmtru
Copy link

In the documentation for encrypt one possible method to specify the cipher is as a Cipher object. However, this throws an error:

julia> using MbedTLS

julia> secret_key = rand(UInt8, 32);

julia> encrypt(Cipher(CIPHER_AES), secret_key, "message")
ERROR: MethodError: no method matching Cipher(::Cipher)
Closest candidates are:
  Cipher() at ~/.julia/packages/MbedTLS/lqmet/src/cipher.jl:93
  Cipher(::Union{MbedTLS.CipherID, MbedTLS.CipherKind}) at ~/.julia/packages/MbedTLS/lqmet/src/cipher.jl:182
  Cipher(::MbedTLS.CipherInfo) at ~/.julia/packages/MbedTLS/lqmet/src/cipher.jl:166
Stacktrace:
 [1] crypt(cipher_info::Cipher, op::MbedTLS.Operation, key::Vector{UInt8}, iv::Nothing, msg::String)
   @ MbedTLS ~/.julia/packages/MbedTLS/lqmet/src/cipher.jl:277
 [2] encrypt(cipher::Cipher, key::Vector{UInt8}, msg::String, iv::Nothing) (repeats 2 times)
   @ MbedTLS ~/.julia/packages/MbedTLS/lqmet/src/cipher.jl:306
 [3] top-level scope
   @ REPL[6]:1

Looking at the source code, it looks like encrypt calls crypt, which calls the Cipher constructor on the cipher_info parameter, which in this case is already a Cipher!

Maybe in the documentation it should say that the cipher can be a CipherInfo object instead of a Cipher object? This works:

julia> encrypt(MbedTLS.CipherInfo(CIPHER_AES), secret_key, "message")
16-element Vector{UInt8}:
 0xbd
 0x5f
 0x2b
 0x20
 0x33
 0xa3
 0x7e
 0x54
 0x86
 0x03
 0x68
 0x26
 0xa9
 0xa2
 0xaf
 0xa5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant