4

下面的示例将读取用户的密码,但也会以纯文本形式回显,有没有办法解决这个问题?

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;
}
4

3 回答 3

6

现在已经实施了。

Stdin.echoMode

import 'dart:io';

void main() {
  print('Enter password: ');
  stdin.echoMode = false;
  var password = stdin.readLineSync();  
  // Do something with password...
}
于 2013-12-18T20:15:12.433 回答
4

在将 tty 控件添加到 dart:io 之前,这是不可能的。与此同时,我建议:

stty -echo
dart ./password.dart
stty echo

我在这里打开了一个错误:https ://code.google.com/p/dart/issues/detail?id=8190

于 2013-01-29T21:05:11.110 回答
0

dart DCli 包提供了一个选项来执行此操作。

https://pub.dev/packages/dcli

import 'package:dcli/dcli.dart';

void main() {
  var password = ask('password', hidden: true);
}
于 2022-02-05T20:56:05.017 回答