These are chat archives for rust-lang/rust

11th
Jun 2017
Sathya Narrayanan
@sourcepirate
Jun 11 2017 05:04
@all I am getting the below error when converting Vec<u8> to String
FromUtf8Error { bytes: [65, 24, 181, 201, 178, 180, 54, 8, 140, 28, 217, 17, 63, 54, 159, 160], error: Utf8Error { valid_up_to: 2 } }
What does this mean
Sathya Narrayanan
@sourcepirate
Jun 11 2017 06:23
fn main() {
    let vecy : Vec<u8>= vec![111, 42, 33, 6, 109, 3, 62, 41, 98, 124, 87, 145, 161, 144, 205, 117];
    println!("{:?}", String::from_utf8(vecy));
}
This is is the code i am using
Denis Lisov
@tanriol
Jun 11 2017 08:02
@sourcepirate Well, that means your byte sequence is not a valid UTF8-encoded string :-)
65 and 24 are valid, but the next one, 181, cannot be the first byte of a character.
Sathya Narrayanan
@sourcepirate
Jun 11 2017 08:05
Oh! thanks. Anyway i kept it as Vec<u8> to procced with function. It works now
@tanriol How can we strip the string off b'\0' character.
I have Hello\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0}\u{0} but i need Hello.
Denis Lisov
@tanriol
Jun 11 2017 08:35
@sourcepirate Are zero bytes in the middle of the string possible?
Sathya Narrayanan
@sourcepirate
Jun 11 2017 08:36
nope
Sathya Narrayanan
@sourcepirate
Jun 11 2017 09:00
only at the ending
Denis Lisov
@tanriol
Jun 11 2017 09:05
What's your use case like? For example, are you supposed to ensurs that there are no zero bytes in the middle, or are they trusted to be correct?
Sathya Narrayanan
@sourcepirate
Jun 11 2017 09:29
My actual scenario is , I am trying to decrypt a message using aes cipher.
Each encrypted string will be padded to the nearest 16 byte string. So while decrypting the message i need to remove the padding which i added earlier. In the above message "Hello" message to be decrypted. But since it is padded i want to remove the padding.
Denis Lisov
@tanriol
Jun 11 2017 09:32
@sourcepirate Wow! Point number one I have to make is never implement crypto yourself unless you're a cryptographer. Your answer sounds like you're not.
Sathya Narrayanan
@sourcepirate
Jun 11 2017 09:34
Yup I am not :) But crypto gave me the padded string.
I am expecting some function like . String.strip()
but i am not able to find it..
Denis Lisov
@tanriol
Jun 11 2017 09:39
Strings do have trim methods, but you seem to be dealing with a byte array.
Sathya Narrayanan
@sourcepirate
Jun 11 2017 09:40
yup!.
Denis Lisov
@tanriol
Jun 11 2017 09:43
Ok, assuming your data was authenticated and thus you're not timing-sensitive at this stage... I'd go with byte_string.iter().take_while(|&c| c != 0).collect::<Vec<_>>()
Sathya Narrayanan
@sourcepirate
Jun 11 2017 09:44
Thanks! I will try it.
Sathya Narrayanan
@sourcepirate
Jun 11 2017 14:06
Is there any good crypto library for diffiehellman key exchange ??
Michal 'vorner' Vaner
@vorner
Jun 11 2017 16:49
@sourcepirate It's possible to make mistakes in putting these crypto pieces together. Is there a reason why you don't use something like gpg (it has a symetric mode as well) or openssl or something already existing?
There are bindings for several crypto libraries (sorry, I don't know the names), there's probably something that is rust-only as well. Some of them will have diffie-hellman for sure.
Sathya Narrayanan
@sourcepirate
Jun 11 2017 16:54
I am trying to exchange cryptographic keys with two machines. I need to implement diffiehellman mechanism. Apart from this once the keys are exchanged I need it to encrypt each messages with AES cipher.
Michal 'vorner' Vaner
@vorner
Jun 11 2017 17:46
Apart from these things, there's a lot more you probably want to do (and don't know it). For example, if done in the „obvious“ way, two messages that are the same will be the same when encrypted. But you probably don't want the attacker to know these two messages were the same. And there's probably bunch of other problems.
That's why it's better to just use something already created.
Fra ns
@snarf95_twitter
Jun 11 2017 18:06
Yeah what do you need it for? If it's anything to be released you should probably use some library.