Refactor key generation and encoding stuff
This commit is contained in:
parent
4e8e5d5194
commit
d4eb144b6c
@ -15,6 +15,29 @@ void main() {
|
|||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<OpenSSHEd25519KeyPair> generateKeyPair() async {
|
||||||
|
final algorithm = Ed25519();
|
||||||
|
|
||||||
|
// Generate a key pair
|
||||||
|
final cryptographyKeyPair = await algorithm.newKeyPair();
|
||||||
|
|
||||||
|
final privateKey = await cryptographyKeyPair.extractPrivateKeyBytes();
|
||||||
|
final publicKey = (await cryptographyKeyPair.extractPublicKey()).bytes;
|
||||||
|
|
||||||
|
final dartsshKeyPair = OpenSSHEd25519KeyPair(
|
||||||
|
Uint8List.fromList(publicKey),
|
||||||
|
Uint8List.fromList(privateKey + publicKey),
|
||||||
|
"",
|
||||||
|
);
|
||||||
|
|
||||||
|
return dartsshKeyPair;
|
||||||
|
}
|
||||||
|
|
||||||
|
String encodePublicKey(OpenSSHEd25519KeyPair keyPair, {String comment = ''}) {
|
||||||
|
final publicKeyBytes = keyPair.toPublicKey().encode();
|
||||||
|
return "ssh-ed25519 ${base64.encode(publicKeyBytes)} $comment";
|
||||||
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
const MyApp({super.key});
|
||||||
|
|
||||||
@ -64,35 +87,10 @@ class MyHomePage extends StatefulWidget {
|
|||||||
State<MyHomePage> createState() => _MyHomePageState();
|
State<MyHomePage> createState() => _MyHomePageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
String encodeOpenSshEd25519PublicKey(
|
|
||||||
Uint8List publicKeyBytes, {
|
|
||||||
String comment = '',
|
|
||||||
}) {
|
|
||||||
Uint8List encodeString(String str) {
|
|
||||||
final strBytes = utf8.encode(str);
|
|
||||||
final lenBytes = ByteData(4)..setUint32(0, strBytes.length);
|
|
||||||
return Uint8List.fromList(lenBytes.buffer.asUint8List() + strBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
Uint8List encodeBytes(Uint8List bytes) {
|
|
||||||
final lenBytes = ByteData(4)..setUint32(0, bytes.length);
|
|
||||||
return Uint8List.fromList(lenBytes.buffer.asUint8List() + bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
final type = 'ssh-ed25519';
|
|
||||||
final keyData = Uint8List.fromList(
|
|
||||||
encodeString(type) + encodeBytes(publicKeyBytes),
|
|
||||||
);
|
|
||||||
final keyBase64 = base64.encode(keyData);
|
|
||||||
|
|
||||||
return '$type $keyBase64${comment.isNotEmpty ? ' $comment' : ''}';
|
|
||||||
}
|
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
int _counter = 0;
|
int _counter = 0;
|
||||||
int navIndex = 0;
|
int navIndex = 0;
|
||||||
List<int> privateKey = [];
|
OpenSSHEd25519KeyPair? keyPair;
|
||||||
List<int> publicKey = [];
|
|
||||||
String _output = '';
|
String _output = '';
|
||||||
String key = '';
|
String key = '';
|
||||||
|
|
||||||
@ -109,16 +107,10 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
if (navIndex == 0) {
|
if (navIndex == 0) {
|
||||||
SSHSocket? socket;
|
SSHSocket? socket;
|
||||||
|
|
||||||
OpenSSHEd25519KeyPair? keypair;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
keypair = OpenSSHEd25519KeyPair(
|
if (keyPair != null) {
|
||||||
Uint8List.fromList(publicKey),
|
print(keyPair!.toPem());
|
||||||
Uint8List.fromList(privateKey + publicKey),
|
}
|
||||||
"",
|
|
||||||
);
|
|
||||||
|
|
||||||
print(keypair.toPem());
|
|
||||||
|
|
||||||
socket = await SSHSocket.connect(
|
socket = await SSHSocket.connect(
|
||||||
'192.168.2.123',
|
'192.168.2.123',
|
||||||
@ -137,7 +129,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
socket,
|
socket,
|
||||||
username: 'test',
|
username: 'test',
|
||||||
//onPasswordRequest: () => '123456',
|
//onPasswordRequest: () => '123456',
|
||||||
identities: List.of(keypair != null ? [keypair] : []),
|
identities: List.of(keyPair != null ? [keyPair!] : []),
|
||||||
);
|
);
|
||||||
|
|
||||||
final uptime = await client.run('uptime');
|
final uptime = await client.run('uptime');
|
||||||
@ -153,16 +145,9 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
|
|
||||||
print("foo");
|
print("foo");
|
||||||
} else {
|
} else {
|
||||||
final algorithm = Ed25519();
|
|
||||||
|
|
||||||
// Generate a key pair
|
// Generate a key pair
|
||||||
final keyPair = await algorithm.newKeyPair();
|
|
||||||
|
|
||||||
privateKey = await keyPair.extractPrivateKeyBytes();
|
keyPair = await generateKeyPair();
|
||||||
publicKey = (await keyPair.extractPublicKey())
|
|
||||||
.bytes; // Extract the public key bytes
|
|
||||||
|
|
||||||
final publicKeyStr = base64.encode(publicKey);
|
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
// private to hex string
|
// private to hex string
|
||||||
@ -170,10 +155,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
// .map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
// .map((byte) => byte.toRadixString(16).padLeft(2, '0'))
|
||||||
// .join('');
|
// .join('');
|
||||||
|
|
||||||
key = encodeOpenSshEd25519PublicKey(
|
if (keyPair != null) {
|
||||||
Uint8List.fromList(publicKey),
|
key = encodePublicKey(keyPair!, comment: "leinelab-app-key");
|
||||||
comment: 'leinelab-app-key',
|
} else {
|
||||||
);
|
key = "No key pair generated.";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await SystemChannels.platform.invokeMethod<void>(
|
await SystemChannels.platform.invokeMethod<void>(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user