bech32 0.8

This commit is contained in:
Jack Grigg 2021-03-21 12:02:11 +13:00
parent 98a4c993b3
commit 9b82182828
6 changed files with 24 additions and 11 deletions

4
Cargo.lock generated
View file

@ -194,9 +194,9 @@ dependencies = [
[[package]] [[package]]
name = "bech32" name = "bech32"
version = "0.7.2" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdcf67bb7ba7797a081cd19009948ab533af7c355d5caf1d08c777582d351e9c" checksum = "6c7f7096bc256f5e5cb960f60dfc4f4ef979ca65abe7fb9d5a4f77150d3783d4"
[[package]] [[package]]
name = "bitflags" name = "bitflags"

View file

@ -10,7 +10,7 @@ edition = "2018"
[dependencies] [dependencies]
age-core = { version = "0.5.0", path = "../age-core", features = ["plugin"] } age-core = { version = "0.5.0", path = "../age-core", features = ["plugin"] }
bech32 = "0.7.2" bech32 = "0.8"
chrono = "0.4" chrono = "0.4"
secrecy = "0.7" secrecy = "0.7"

View file

@ -159,6 +159,7 @@
#![deny(intra_doc_link_resolution_failure)] #![deny(intra_doc_link_resolution_failure)]
#![deny(missing_docs)] #![deny(missing_docs)]
use bech32::Variant;
use secrecy::SecretString; use secrecy::SecretString;
use std::io; use std::io;
@ -184,6 +185,7 @@ pub fn print_new_identity(plugin_name: &str, identity: &[u8], recipient: &[u8])
bech32::encode( bech32::encode(
&format!("{}{}", PLUGIN_RECIPIENT_PREFIX, plugin_name), &format!("{}{}", PLUGIN_RECIPIENT_PREFIX, plugin_name),
recipient.to_base32(), recipient.to_base32(),
Variant::Bech32
) )
.expect("HRP is valid") .expect("HRP is valid")
); );
@ -192,6 +194,7 @@ pub fn print_new_identity(plugin_name: &str, identity: &[u8], recipient: &[u8])
bech32::encode( bech32::encode(
&format!("{}{}-", PLUGIN_IDENTITY_PREFIX, plugin_name), &format!("{}{}-", PLUGIN_IDENTITY_PREFIX, plugin_name),
identity.to_base32(), identity.to_base32(),
Variant::Bech32,
) )
.expect("HRP is valid") .expect("HRP is valid")
.to_uppercase() .to_uppercase()

View file

@ -40,7 +40,7 @@ scrypt = { version = "0.6", default-features = false }
rand = "0.7" rand = "0.7"
# - Key encoding # - Key encoding
bech32 = "0.7.2" bech32 = "0.8"
# OpenSSH-specific dependencies: # OpenSSH-specific dependencies:
# - RSAES-OAEP from RFC 8017 with SHA-256 and MGF1 # - RSAES-OAEP from RFC 8017 with SHA-256 and MGF1

View file

@ -1,4 +1,4 @@
use bech32::FromBase32; use bech32::{FromBase32, Variant};
#[cfg(all(any(feature = "armor", feature = "cli-common"), windows))] #[cfg(all(any(feature = "armor", feature = "cli-common"), windows))]
pub(crate) const LINE_ENDING: &str = "\r\n"; pub(crate) const LINE_ENDING: &str = "\r\n";
@ -6,9 +6,13 @@ pub(crate) const LINE_ENDING: &str = "\r\n";
pub(crate) const LINE_ENDING: &str = "\n"; pub(crate) const LINE_ENDING: &str = "\n";
pub(crate) fn parse_bech32(s: &str) -> Option<(String, Vec<u8>)> { pub(crate) fn parse_bech32(s: &str) -> Option<(String, Vec<u8>)> {
bech32::decode(s) bech32::decode(s).ok().and_then(|(hrp, data, variant)| {
.ok() if let Variant::Bech32 = variant {
.and_then(|(hrp, data)| Vec::from_base32(&data).ok().map(|d| (hrp, d))) Vec::from_base32(&data).ok().map(|d| (hrp, d))
} else {
None
}
})
} }
pub(crate) mod read { pub(crate) mod read {

View file

@ -4,7 +4,7 @@ use age_core::{
format::{FileKey, Stanza, FILE_KEY_BYTES}, format::{FileKey, Stanza, FILE_KEY_BYTES},
primitives::{aead_decrypt, aead_encrypt, hkdf}, primitives::{aead_decrypt, aead_encrypt, hkdf},
}; };
use bech32::ToBase32; use bech32::{ToBase32, Variant};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use secrecy::SecretString; use secrecy::SecretString;
@ -64,7 +64,8 @@ impl Identity {
pub fn to_string(&self) -> SecretString { pub fn to_string(&self) -> SecretString {
let mut sk_bytes = self.0.to_bytes(); let mut sk_bytes = self.0.to_bytes();
let sk_base32 = sk_bytes.to_base32(); let sk_base32 = sk_bytes.to_base32();
let mut encoded = bech32::encode(SECRET_KEY_PREFIX, sk_base32).expect("HRP is valid"); let mut encoded =
bech32::encode(SECRET_KEY_PREFIX, sk_base32, Variant::Bech32).expect("HRP is valid");
let ret = SecretString::new(encoded.to_uppercase()); let ret = SecretString::new(encoded.to_uppercase());
// Clear intermediates // Clear intermediates
@ -151,7 +152,12 @@ impl fmt::Display for Recipient {
write!( write!(
f, f,
"{}", "{}",
bech32::encode(PUBLIC_KEY_PREFIX, self.0.as_bytes().to_base32()).expect("HRP is valid") bech32::encode(
PUBLIC_KEY_PREFIX,
self.0.as_bytes().to_base32(),
Variant::Bech32
)
.expect("HRP is valid")
) )
} }
} }