rust


How do I create an empty byte array?


How can I create an empty [u8] that I can then pass to an openssl function so it can write it's result to it?
fn test_encrypt(key_pair: openssl::rsa::Rsa) {
let text = b"Hello";
println!("{:?}", text);
let mut result = b" ";
let pad = openssl::rsa::Padding::from_raw(1);
key_pair.private_encrypt(text, result, pad);
}
My code currently results in the following error:
error[E0308]: mismatched types
--> src/main.rs:20:36
|
20 | key_pair.private_encrypt(text, result, pad);
| ^^^^^^ types differ in mutability
|
= note: expected type `&mut [u8]`
found type `&[u8; 0]`
Reading the error messages will help you far more; mismatched types, types differ in mutability, expected type &mut [u8], found type &[u8; 0]. You problem isn't how to create an array, it's that you aren't creating the right type of array.
Printing the type of the variable will show that b" " creates a &[u8; 1] — an immutable reference to an array. It doesn't matter what you try, you won't be able to write into this data. In fact, it's stored in the static data in the compiled executable.
Instead, you need something that can be used as a mutable slice. The two primary ways are to create an actual array or a Vec:
// Array
let mut result = [0; 24];
key_pair.private_encrypt(text, &mut result, pad);
// Vec
let mut result = vec![0; 24];
key_pair.private_encrypt(text, &mut result, pad);
An array is stack-allocated and has a fixed size and capacity which must be equal. A vector is heap-allocated, has a dynamic size and capacity that may differ from each other.
Both of these can be dereferenced to a &mut [u8].
Picking the appropriate size for your array or vector is something that is application specific. Checking the OpenSSL documentation is the right course of action.
The private_encrypt() method expects a mutable slice of a sufficiently long buffer (key_pair.size() bytes). Change
let mut result = b" ";
key_pair.private_encrypt(text, result, pad);
to
let mut result = vec![0; key_pair.size()];
key_pair.private_encrypt(text, &mut result, pad);

Related Links

How do I shuffle a VecDeque?
Can I use a mutable reference method like a value-passing one?
How to execute Rust code directly on Unix systems? (using the shebang)
Convenient 'Option<Box<Any>>' access when success is assured?
Use a closure inside a thread
How to change the variable from inside Fn closure in Rust?
Create mutual reference for rust [duplicate]
How do I safely use an object while it is mutably borrowed?
Workspace giving two different behaviors in Rust
Restricting object lifetimes in Rust
Project with serde cannot compile
Cannot infer an appropriate lifetime for autoref due to conflicting requirements
How to define a hashmap with contents in 1 line in Rust? [duplicate]
How solve “cannot index a value of type `usize`” error?
Preserve lifetime of an argument in another object [duplicate]
How can I use multiple items in a Vec at a time in Rust?

Categories

HOME
flask
gaming
freeradius
antd
risk-management
informatica
cryptography
lvm
mosquitto
dji-sdk
clone
save
tfs2010
swarm
hystrix
tiff
mahout-recommender
word2vec
varnish-vcl
http-authentication
raml
libgit2
substring
pdf.js
hdmi
tf-idf
marquee
django-import-export
sfml
lxml
microsoft-certifications
om-next
android-canvas
numerics
redgate
runtimeexception
utf
jacoco-maven-plugin
uipickerview
distributed-caching
hypothesis-test
opal-framework
oracle-bmcs
common-table-expression
windows-media-player
deployd
beeline
password-hash
winston
maven-jaxb2-plugin
mura
dex
alertify
elastic4s
fuzzer
noclassdeffounderror
mathnet
xcopy
psd
outlook.com
nette
http.client
blank-line
extend
netdatacontractserializer
ngcordova
uitest
whitespace-language
fiware-bosun
directorysearcher
jcheckbox
string.format
visual-studio-6
nidaqmx
nachos
mft
rikulo
ofstream
cyrillic
smartfox
jquery-dialog
daap
story
clrprofiler
msinfo32
first-class
zotonic
liveconnect
dropshadow
background-repeat
data-dump
qt-mobility
mediacontroller
nsconnection
downgrade
android-input-method
fgetc
fuzzy-comparison
autobench
twitter-feed
html-generation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App