Extract base64 logic to module

This commit is contained in:
Tim Visée 2018-03-08 11:26:07 +01:00
parent 396cf97963
commit 05d22c57bc
No known key found for this signature in database
GPG key ID: A28432A0AE6E6306
4 changed files with 31 additions and 9 deletions

7
Cargo.lock generated
View file

@ -184,6 +184,7 @@ dependencies = [
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
"sha2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"version-compare 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -949,6 +950,11 @@ name = "vec_map"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version-compare"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.3"
@ -1107,6 +1113,7 @@ dependencies = [
"checksum uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc7e3b898aa6f6c08e5295b6c89258d1331e9ac578cc992fb818759951bdc22"
"checksum vcpkg 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9e0a7d8bed3178a8fb112199d466eeca9ed09a14ba8ad67718179b4fd5487d0b"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum version-compare 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "78068add8bf1e4d37d13fa5867182fe4c03f8e525c831053733f83aaba942d37"
"checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"

View file

@ -17,3 +17,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
sha2 = "0.7"
version-compare = "0.0"

19
src/b64.rs Normal file
View file

@ -0,0 +1,19 @@
//! A simple module for encoding or decoding a base64 string from or to a
//! byte array.
//!
//! This module uses an URL-safe scheme, and doesn't add additional padding
//! to the encoded strings.
extern crate base64;
use self::base64::DecodeError;
/// Encode the given byte slice using base64, in an URL-safe manner.
pub fn encode(input: &[u8]) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}
/// Decode the given string as base64, in an URL-safe manner.
pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
base64::decode_config(input, base64::URL_SAFE_NO_PAD)
}

View file

@ -1,4 +1,3 @@
extern crate base64;
extern crate clap;
extern crate hkdf;
extern crate hyper;
@ -12,6 +11,7 @@ extern crate serde_derive;
extern crate serde_json;
extern crate sha2;
mod b64;
mod reader;
use std::fmt;
@ -124,7 +124,7 @@ fn main() {
// Make the request
let mut res = client.post("http://localhost:8080/api/upload")
.header(Authorization(format!("send-v1 {}", base64_encode(&auth_key))))
.header(Authorization(format!("send-v1 {}", b64::encode(&auth_key))))
.header(XFileMetadata::from(&metadata))
.multipart(form)
.send()
@ -136,7 +136,7 @@ fn main() {
// Print the response
let url = upload_res.download_url(&secret);
println!("Response: {:#?}", upload_res);
println!("Secret key: {}", base64_encode(&secret));
println!("Secret key: {}", b64::encode(&secret));
println!("Download URL: {}", url);
// Open the URL in the browser
@ -285,11 +285,6 @@ impl UploadResponse {
///
/// The secret bytes must be passed to `secret`.
pub fn download_url(&self, secret: &[u8]) -> String {
format!("{}#{}", self.url, base64_encode(secret))
format!("{}#{}", self.url, b64::encode(secret))
}
}
/// Encode the given byte slice using base64, in an URL-safe manner.
fn base64_encode(input: &[u8]) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
}