1 unstable release

0.1.0 May 18, 2020

#14 in #stdin

Download history 4/week @ 2021-08-15 5/week @ 2021-08-22 2/week @ 2021-09-05 27/week @ 2021-09-12 19/week @ 2021-09-19 57/week @ 2021-09-26 16/week @ 2021-10-03 27/week @ 2021-10-10 22/week @ 2021-10-17 12/week @ 2021-10-24 5/week @ 2021-10-31 6/week @ 2021-11-07 31/week @ 2021-11-14 8/week @ 2021-11-21 9/week @ 2021-11-28

54 downloads per month

MIT/Apache

11KB
128 lines

scanpw

Read a password from standard input


Overview

scanpw provides a macro and a function (for more granular error handling) to facilitate reading passwords from standard input in a secure manner. It expands to an expression that returns a String, so it can be assigned to a variable or used directly. The macro may take arguments like those to print, which can be used to generate a prompt.

Examples

Simple prompt

let password = scanpw!("Password: ");

This results in a prompt that looks like this (where _ represents where the user will start typing):

Password: _

No prompt

let password = scanpw!();

Formatted prompt

let password = scanpw!("Password for {}: ", username);

Custom echo behavior

If the first argument to scanpw is an expression of type Option<char> instead of a string literal, it is used to either set a custom replacement character (like Some('X')) or disable echoing entirely (like None). For example:

// Don't print a '*' for each character the user types
let echo_settings: Option<char> = None;

let password = scanpw!(echo_settings, "Password: ");

The default behavior is to echo *s for each character entered.

Dependencies

~0.8–1.3MB
~26K SLoC

^