How can I change the return type of this function?

Erik

I'm going through the matasano crypto challenges using rust, with rust-crypto for the AES implementation. I have this function to do basic ECB mode encryption (basically taken nearly verbatim from the rust-crypto repository's example):

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(e) => return Err(e)
        }
    }

    Ok(final_result)
}

The above version compiles with no problem, and works as expected. However, to make it fit with the rest of my error handling scheme I'd like to change the return type to Result<Vec<u8>,&'static str>. This is the function with that change applied:

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(_) => return Err("Encryption failed")
        }
    }

    Ok(final_result)
}

When I attempt to compile this version, I get the following error (paths removed for clarity):

error: source trait is private
         let result = encryptor.encrypt(&mut read_buffer,
                                        &mut write_buffer,
                                        true);
error: source trait is private
let r = decryptor.decrypt(&mut read_buffer, &mut write_buffer, true);
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only way I've been able to change this type is to wrap the original function in a conversion function like this:

pub fn converted_enc(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
   match aes_enc_ecb_128(key,data) {
       Ok(v) => Ok(v),
       Err(_) => Err("Encryption failed")
   } 
}

What should I do instead of the above in order to get the return value to fit with the rest of my API, and why is the more direct method failing?

I'm using the following versions of rust/cargo:

rustc 1.2.0-nightly (0cc99f9cc 2015-05-17) (built 2015-05-18)
cargo 0.2.0-nightly (ac61996 2015-05-17) (built 2015-05-17)
aochagavia

I think you have come across a bug of the compiler. Your code should compile

You can use crypto::symmetriccipher::Encryptor; as a workaround:

pub fn aes_enc_ecb_128(key: &[u8], data: &[u8]) 
                       -> Result<Vec<u8>, &'static str> {
    use crypto::symmetriccipher::Encryptor;
    let mut encryptor = aes::ecb_encryptor(
            aes::KeySize::KeySize128,
            key,
            blockmodes::NoPadding);
    let mut final_result = Vec::<u8>::new();
    let mut read_buffer = buffer::RefReadBuffer::new(data);
    let mut buffer = [0; 4096];
    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);

    loop {
        let result = encryptor.encrypt(&mut read_buffer,
                                       &mut write_buffer,
                                       true);

        final_result.extend(write_buffer
                            .take_read_buffer()
                            .take_remaining().iter().map(|&i| i));
        match result {
            Ok(BufferResult::BufferUnderflow) => break,
            Ok(_) => {},
            Err(_) => return Err("Encryption failed")
        }
    }

    Ok(final_result)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Can a function return a type?

From Dev

Can I make a C function return an arbitrary type?

From Dev

How can I return a function?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I determine the return type of a C++11 member function

From Dev

How can I specify a return type for operator[]?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I inject a type into a function in Typescript?

From Dev

How to infer the type of the function that can return type depending on condition?

From Dev

Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

From Dev

How can I return a called back function?

From Dev

how can i return a dataframe from a function

From Dev

How can I shorten a type in the body of a function

From Dev

How can I type-hint a function where the return type depends on the input type of an argument?

From Dev

Why can't I return the generic type T from this function?

From Dev

how can i handle images and change there function?

From Dev

How do I return by value for an "auto" return type function

From Dev

How can I use SFINAE to prioritise a non-templated function while also providing a return type?

From Dev

How can I return a function?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I specify a return type for operator[]?

From Dev

How can I inject a type into a function in Typescript?

From Dev

Cannot call value of non-function type 'JSON', How can I return value from block in Swift

From Dev

How can I change user inside function

From Dev

How can I return a called back function?

From Dev

how can i return the count from a function

From Dev

How can I sent the type to function

From Dev

How Can I change Image type with NodeJS?

Related Related

  1. 1

    Can a function return a type?

  2. 2

    Can I make a C function return an arbitrary type?

  3. 3

    How can I return a function?

  4. 4

    How can I define a return type of void for a function in a Typescript interface?

  5. 5

    How can I define the return type of a lodash reduce function with Typescript?

  6. 6

    How can I determine the return type of a C++11 member function

  7. 7

    How can I specify a return type for operator[]?

  8. 8

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  9. 9

    How can I inject a type into a function in Typescript?

  10. 10

    How to infer the type of the function that can return type depending on condition?

  11. 11

    Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

  12. 12

    How can I return a called back function?

  13. 13

    how can i return a dataframe from a function

  14. 14

    How can I shorten a type in the body of a function

  15. 15

    How can I type-hint a function where the return type depends on the input type of an argument?

  16. 16

    Why can't I return the generic type T from this function?

  17. 17

    how can i handle images and change there function?

  18. 18

    How do I return by value for an "auto" return type function

  19. 19

    How can I use SFINAE to prioritise a non-templated function while also providing a return type?

  20. 20

    How can I return a function?

  21. 21

    How can I define the return type of a lodash reduce function with Typescript?

  22. 22

    How can I specify a return type for operator[]?

  23. 23

    How can I inject a type into a function in Typescript?

  24. 24

    Cannot call value of non-function type 'JSON', How can I return value from block in Swift

  25. 25

    How can I change user inside function

  26. 26

    How can I return a called back function?

  27. 27

    how can i return the count from a function

  28. 28

    How can I sent the type to function

  29. 29

    How Can I change Image type with NodeJS?

HotTag

Archive