mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 03:19:11 +08:00
RsaKeyPair: Make sure we parse public keys correctly
This commit is contained in:
@ -9,6 +9,8 @@ import 'package:steel_crypt/PointyCastleN/random/fortuna_random.dart';
|
|||||||
import 'package:steel_crypt/steel_crypt.dart';
|
import 'package:steel_crypt/steel_crypt.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
|
||||||
|
import 'package:ssh_key/ssh_key.dart' as ssh_key;
|
||||||
|
|
||||||
class RsaKeyPair {
|
class RsaKeyPair {
|
||||||
RSAPublicKey publicKey;
|
RSAPublicKey publicKey;
|
||||||
RSAPrivateKey privateKey;
|
RSAPrivateKey privateKey;
|
||||||
@ -18,8 +20,30 @@ class RsaKeyPair {
|
|||||||
@required String publicKey,
|
@required String publicKey,
|
||||||
}) {
|
}) {
|
||||||
var encrypter = RsaCrypt();
|
var encrypter = RsaCrypt();
|
||||||
publicKey = encrypter.parseKeyFromString(publicKey);
|
|
||||||
privateKey = encrypter.parseKeyFromString(privateKey);
|
publicKey = publicKey.trim();
|
||||||
|
try {
|
||||||
|
var key = ssh_key.publicKeyDecode(publicKey);
|
||||||
|
if (key is ssh_key.RSAPublicKeyWithInfo) {
|
||||||
|
this.publicKey = RSAPublicKey(key.modulus, key.exponent);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
if (publicKey == null) {
|
||||||
|
try {
|
||||||
|
this.publicKey = encrypter.parseKeyFromString(publicKey);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.privateKey = encrypter.parseKeyFromString(privateKey);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RsaKeyPair.generate() {
|
RsaKeyPair.generate() {
|
||||||
|
23
pubspec.lock
23
pubspec.lock
@ -553,6 +553,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.1"
|
version: "1.0.1"
|
||||||
|
pointycastle:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: pointycastle
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.2"
|
||||||
pool:
|
pool:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -733,6 +740,15 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.0+1"
|
version: "1.0.0+1"
|
||||||
|
ssh_key:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
path: "."
|
||||||
|
ref: HEAD
|
||||||
|
resolved-ref: "60e088a136a62cd1afe553e8858558f76df00f2d"
|
||||||
|
url: "https://github.com/GitJournal/ssh_key.git"
|
||||||
|
source: git
|
||||||
|
version: "0.5.0"
|
||||||
stack_trace:
|
stack_trace:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -810,6 +826,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.9.5+1"
|
version: "1.9.5+1"
|
||||||
|
tuple:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: tuple
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.3"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -46,6 +46,8 @@ dependencies:
|
|||||||
equatable: ^1.1.0
|
equatable: ^1.1.0
|
||||||
purchases_flutter: ^1.1.0
|
purchases_flutter: ^1.1.0
|
||||||
cached_network_image: ^2.1.0+1
|
cached_network_image: ^2.1.0+1
|
||||||
|
ssh_key:
|
||||||
|
git: https://github.com/GitJournal/ssh_key.git
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_launcher_icons: "^0.7.2"
|
flutter_launcher_icons: "^0.7.2"
|
||||||
|
@ -8,7 +8,6 @@ void main() {
|
|||||||
expect(pair.isValid(), true);
|
expect(pair.isValid(), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
/*
|
|
||||||
var publicKeySingleLine =
|
var publicKeySingleLine =
|
||||||
'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAgEAiL0jjDdFqK/kYThqKt7THrjABTPWvXmB3URIpGKCP/jZlSuCUP3Oc+IxuFeXSIMvVIYeW2PZAjXQGTn60XzPHr+M0NoGcPAvzZf2u57aX3YKaL93cZSBHR97H+XhcYdrm7ATwfjMDgfgj7+VTvW4nI46Z+qjxmYifc8uVELolg1TDHWY789ggcdvy92oGjB0VUgMEywrOP+LS0DgG4dmkoUBWGP9dvYcPZDUF4q0XY9ZHhvyPWEZ3o2vETTrEJr9QHYwgjmFfJn2VFNnD/4qeDDHOmSlDgEOfQcZIm+XUOn9eVsv//dAPSY/yMJXf8d0ZSm+VS29QShMjA4R+7yh5WhsIhouBRno2PpEVVb37Xwe3V6U3o9UnQ3ADtL75DbrZ5beNWcmKzlJ7jVX5QzHSBAnePbBx/fyeP/f144xPtJWB3jW/kXjtPyWjpzGndaPQ0WgXkbf8fvIuB3NJTTcZ7PeIKnLaMIzT5XNCR+xobvdC8J9d6k84/q/laJKF3G8KbRGPNwnoVg1cwWFez+dzqo2ypcTtv/20yAmz86EvuohZoWrtoWvkZLCoyxdqO93ymEjgHAn2bsIWyOODtXovxAJqPgk3dxM1f9PAEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX09g6nhbk=';
|
'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAgEAiL0jjDdFqK/kYThqKt7THrjABTPWvXmB3URIpGKCP/jZlSuCUP3Oc+IxuFeXSIMvVIYeW2PZAjXQGTn60XzPHr+M0NoGcPAvzZf2u57aX3YKaL93cZSBHR97H+XhcYdrm7ATwfjMDgfgj7+VTvW4nI46Z+qjxmYifc8uVELolg1TDHWY789ggcdvy92oGjB0VUgMEywrOP+LS0DgG4dmkoUBWGP9dvYcPZDUF4q0XY9ZHhvyPWEZ3o2vETTrEJr9QHYwgjmFfJn2VFNnD/4qeDDHOmSlDgEOfQcZIm+XUOn9eVsv//dAPSY/yMJXf8d0ZSm+VS29QShMjA4R+7yh5WhsIhouBRno2PpEVVb37Xwe3V6U3o9UnQ3ADtL75DbrZ5beNWcmKzlJ7jVX5QzHSBAnePbBx/fyeP/f144xPtJWB3jW/kXjtPyWjpzGndaPQ0WgXkbf8fvIuB3NJTTcZ7PeIKnLaMIzT5XNCR+xobvdC8J9d6k84/q/laJKF3G8KbRGPNwnoVg1cwWFez+dzqo2ypcTtv/20yAmz86EvuohZoWrtoWvkZLCoyxdqO93ymEjgHAn2bsIWyOODtXovxAJqPgk3dxM1f9PAEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX09g6nhbk=';
|
||||||
var publicKeySsh2 = '''---- BEGIN SSH2 PUBLIC KEY ----
|
var publicKeySsh2 = '''---- BEGIN SSH2 PUBLIC KEY ----
|
||||||
@ -27,7 +26,7 @@ AEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX0
|
|||||||
9g6nhbk=
|
9g6nhbk=
|
||||||
---- END SSH2 PUBLIC KEY ----''';
|
---- END SSH2 PUBLIC KEY ----''';
|
||||||
|
|
||||||
test('Should write public key string correctly', () async {
|
test('Should read from OpenSSH format', () async {
|
||||||
var pair = RsaKeyPair.fromStrings(
|
var pair = RsaKeyPair.fromStrings(
|
||||||
publicKey: publicKeySsh2,
|
publicKey: publicKeySsh2,
|
||||||
privateKey: "",
|
privateKey: "",
|
||||||
@ -36,7 +35,14 @@ AEQwc1bG+Z/Gc1Fd8DncgxyhKSQzLsfWroTnIn8wsnmhPJtaZWNuT5BJa8GhnzX0
|
|||||||
expect(pair.publicKeyString(), publicKeySingleLine);
|
expect(pair.publicKeyString(), publicKeySingleLine);
|
||||||
});
|
});
|
||||||
|
|
||||||
*/
|
test('Should read from singleline format', () async {
|
||||||
|
var pair = RsaKeyPair.fromStrings(
|
||||||
|
publicKey: publicKeySingleLine,
|
||||||
|
privateKey: "",
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(pair.publicKeyString(), publicKeySingleLine);
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
var privateOpenSshKey = '''-----BEGIN OPENSSH PRIVATE KEY-----
|
var privateOpenSshKey = '''-----BEGIN OPENSSH PRIVATE KEY-----
|
||||||
|
Reference in New Issue
Block a user