2 releases (1 stable)

1.0.0 May 7, 2023
0.1.0 May 18, 2020

#836 in Command-line interface

Download history 1/week @ 2024-11-27 9/week @ 2024-12-04 34/week @ 2024-12-11 15/week @ 2024-12-18 12/week @ 2024-12-25 4/week @ 2025-01-15 104/week @ 2025-01-29 17/week @ 2025-02-05 53/week @ 2025-02-12 19/week @ 2025-02-26

184 downloads per month
Used in crackle

MIT/Apache

15KB
178 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

~3–43MB
~585K SLoC