Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable stdin echo in a dart console application for password input

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;
}
like image 325
adam-singer Avatar asked Dec 26 '22 10:12

adam-singer


2 Answers

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...
}
like image 146
Greg Lowe Avatar answered Jan 13 '23 15:01

Greg Lowe


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

like image 23
Cutch Avatar answered Jan 13 '23 13:01

Cutch