From 1d1cc940f47c41637adea5c5a1e5d3c80807f9d7 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 6 Jan 2019 00:23:27 +0100 Subject: Add the base32 format for OTP secrets Many applications display OTP secrets in the base32 format (according to RFC 4648). This patch adds base32 as a possible value for the --format option to the otp set subcommand. --- nitrocli/src/commands.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'nitrocli/src/commands.rs') diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index 71b2bdd..289c257 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -457,6 +457,13 @@ fn prepare_ascii_secret(secret: &str) -> Result { } } +/// Prepare a base32 secret string for libnitrokey. +fn prepare_base32_secret(secret: &str) -> Result { + base32::decode(base32::Alphabet::RFC4648 { padding: false }, secret) + .map(|vec| format_bytes(&vec)) + .ok_or_else(|| Error::Error("Could not parse base32 secret".to_string())) +} + /// Configure a one-time password slot on the Nitrokey device. pub fn otp_set( ctx: &args::ExecCtx, @@ -468,6 +475,7 @@ pub fn otp_set( ) -> Result<()> { let secret = match secret_format { args::OtpSecretFormat::Ascii => prepare_ascii_secret(&data.secret)?, + args::OtpSecretFormat::Base32 => prepare_base32_secret(&data.secret)?, args::OtpSecretFormat::Hex => data.secret, }; let data = nitrokey::OtpSlotData { secret, ..data }; -- cgit v1.2.1