Question

The example below will read in a user's password but also echo it in plain text, is there a way around this?

Future<String> promptPassword() {
  var completer = new Completer<String>();
  var stream = new StringInputStream(stdin);
  stdout.writeString("Warning: Password will be displayed here until I find a better way to do this.\n");
  stdout.writeString("Watch your back...\n");
  stdout.writeString("GitHub password for $gituser: ");
  stream.onLine = () {
    var str = stream.readLine();
    stdin.close();
    completer.complete(str);
  };
  return completer.future;
}
Was it helpful?

Solution

This has been implemented now.

See Stdin.echoMode

import 'dart:io';

void main() {
  print('Enter password: ');
  stdin.echoMode = false;
  var password = stdin.readLineSync();  
  // Do something with password...
}

OTHER TIPS

This is not possible until tty control is added to dart:io. In the meantime, I recommend:

stty -echo
dart ./password.dart
stty echo

I have opened a bug here: https://code.google.com/p/dart/issues/detail?id=8190

The dart DCli package provide an option to do this.

https://pub.dev/packages/dcli

import 'package:dcli/dcli.dart';

void main() {
  var password = ask('password', hidden: true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top