From 3a5aa38ddf4d0fc0d7b791639f69d08b6ac3c613 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 7 Jun 2021 23:43:26 +0200 Subject: [PATCH] Experiment: Try to generate ECC keys in Dart I'm stuck at how to convert them between two formats. --- lib/ssh/keygen.dart | 20 ++++++++++++++++++++ lib/ssh/main_keygen.dart-exp | 24 ++++++++++++++++++++++++ pubspec.lock | 23 ++++++++--------------- pubspec.yaml | 2 +- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/lib/ssh/keygen.dart b/lib/ssh/keygen.dart index baee33e7..955ec95e 100644 --- a/lib/ssh/keygen.dart +++ b/lib/ssh/keygen.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:basic_utils/basic_utils.dart'; import 'package:git_bindings/git_bindings.dart' as gb; import 'package:path/path.dart' as p; @@ -76,3 +77,22 @@ Future generateSSHKeysNative({required String comment}) async { return null; } + +Future generateSSHEccKeys({required String comment}) async { + print("Generating KeyPair ..."); + var stopwatch = Stopwatch()..start(); + var keyPair = CryptoUtils.generateEcKeyPair(curve: 'secp384r1'); + var publicKey = keyPair.publicKey as ECPublicKey; + var privateKey = keyPair.privateKey as ECPrivateKey; + print("Generating KeyPair took: ${stopwatch.elapsed}"); + + var publicPem = CryptoUtils.encodeEcPublicKeyToPem(publicKey); + var privatePem = CryptoUtils.encodeEcPrivateKeyToPem(privateKey); + + // FIXME: I need to learn to convert from the public key PEM format to ecdsa-sha2-nistp384 + return SshKey( + publicKey: publicPem, + privateKey: privatePem, + password: "", + ); +} diff --git a/lib/ssh/main_keygen.dart-exp b/lib/ssh/main_keygen.dart-exp index 5cd5f7b2..8741257d 100644 --- a/lib/ssh/main_keygen.dart-exp +++ b/lib/ssh/main_keygen.dart-exp @@ -55,3 +55,27 @@ String privateKeyString() { // -> https://golang.org/src/crypto/x509/pkcs8.go // Key format is PKCS8 or OpenSSH. + +/* +String publicKeyString(ECPublicKey publicKey, String comment) { + var publicPem = CryptoUtils.encodeEcPublicKeyToPem(publicKey); + + print('public PEM'); + print(publicPem); + print('\n'); + + var publicKeyBytes = CryptoUtils.getBytesFromPEMString(publicPem); + var data = BinaryLengthValue.encode([ + BinaryLengthValue.fromString("ecdsa-sha2-nistp384"), + BinaryLengthValue(publicKeyBytes), + ]); + + if (comment.isNotEmpty) { + comment = comment.replaceAll('\r', ' '); + comment = comment.replaceAll('\n', ' '); + comment = ' $comment'; + } + + return 'ecdsa-sha2-nistp384 ${base64.encode(data)}$comment'; +} +*/ diff --git a/pubspec.lock b/pubspec.lock index 2f95a758..d5334caa 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -29,13 +29,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0" - asn1lib: - dependency: transitive - description: - name: asn1lib - url: "https://pub.dartlang.org" - source: hosted - version: "1.0.0" async: dependency: transitive description: @@ -57,6 +50,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.1" + basic_utils: + dependency: "direct main" + description: + name: basic_utils + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" benchmark_harness: dependency: "direct dev" description: @@ -246,13 +246,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.1" - crypton: - dependency: "direct main" - description: - name: crypton - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.1" csv: dependency: transitive description: @@ -949,7 +942,7 @@ packages: name: pointycastle url: "https://pub.dartlang.org" source: hosted - version: "3.0.1" + version: "3.1.1" pool: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7e652d89..e56ba38d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,10 +23,10 @@ dependencies: git: https://github.com/GitJournal/icloud_documents_path.git auto_size_text: ^3.0.0-nullsafety.0 badges: ^2.0.1 + basic_utils: ^3.1.0 connectivity: ^3.0.3 # cryptography: ^1.4.1 # cryptography_flutter: ^1.0.0 - crypton: ^2.0.1 dashbook: ^0.1.2 device_info: ^2.0.0 dots_indicator: ^2.0.0