似乎Paramiko的RSA文件有问题。要么是,要么是我不明白。这是我所做的。首先,我生成了密钥对:
public class FirebaseDocumentLiveData extends LiveData<DocumentSnapshot> {
// Logging constant
private static final String TAG = "FirebaseQueryLiveData";
// Document Reference
private final DocumentReference documentReference;
// Listener
private final MyDocumentListener listener = new MyDocumentListener();
// Handler
private final Handler handler = new Handler();
private ListenerRegistration listenerRegistration;
// Flag to remove listener
private boolean listenerRemovePending = false;
// Remove listener runnable
private final Runnable removeListener = new Runnable() {
@Override
public void run() {
listenerRegistration.remove();
listenerRemovePending = false;
}
};
// Constructor
public FirebaseDocumentLiveData(DocumentReference documentReference) {
this.documentReference = documentReference;
}
// On active
@Override
protected void onActive() {
super.onActive();
Log.d(TAG, "onActive");
// Check flag
if (listenerRemovePending) {
// Remove callbacks
handler.removeCallbacks(removeListener);
} else {
// Add listener
listenerRegistration = documentReference.addSnapshotListener(listener);
}
// Update flag
listenerRemovePending = false;
}
// On inactive
@Override
protected void onInactive() {
super.onInactive();
Log.d(TAG, "onInactive");
// Listener removal is schedule on a two second delay
handler.postDelayed(removeListener, 2000);
// Update flag
listenerRemovePending = true;
}
// Listener definition
private class MyDocumentListener implements EventListener<DocumentSnapshot> {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
Log.d(TAG, "onEvent");
// Check for error
if (e != null) {
// Log
Log.d(TAG, "Can't listen to query snapshots: " + documentSnapshot
+ ":::" + e.getMessage());
return;
}
// Set value if listening is successful
setValue(documentSnapshot);
}
}
}
然后我尝试导入该文件:
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/me/.ssh/id_rsa): /tmp/test
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /tmp/test.
Your public key has been saved in /tmp/test.pub.
The key fingerprint is:
SHA256:Xfz89Pi/p+YccZzfUULJiyssLJ/lRs/voTETLyOT25Q me@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ... |
| . .o |
| o....|
| . ..o.oo|
| .S.. ..+o+|
| . o =..+ *=|
| o *++E = =|
| o o*oO.+.|
| .. o=*o*|
+----[SHA256]-----+
我看不到我可能做错了什么。 paramiko版本是2.4.2
我了解到黑客here在其中通过将“ OPENSSH”更改为“ RSA”来编辑密钥文件,但这也不起作用。
私钥的内容:
$ python3
Python 3.7.0 (default, Aug 30 2018, 14:32:33)
[GCC 8.2.1 20180801 (Red Hat 8.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> p = paramiko.RSAKey.from_private_key_file('/tmp/test')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/paramiko/pkey.py", line 206, in from_private_key_file
key = cls(filename=filename, password=password)
File "/usr/local/lib/python3.7/site-packages/paramiko/rsakey.py", line 55, in __init__
self._from_private_key_file(filename, password)
File "/usr/local/lib/python3.7/site-packages/paramiko/rsakey.py", line 175, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File "/usr/local/lib/python3.7/site-packages/paramiko/pkey.py", line 279, in _read_private_key_file
data = self._read_private_key(tag, f, password)
File "/usr/local/lib/python3.7/site-packages/paramiko/pkey.py", line 289, in _read_private_key
raise SSHException("not a valid " + tag + " private key file")
paramiko.ssh_exception.SSHException: not a valid RSA private key file