Skip to content

Commit

Permalink
Remove unused code in util package (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsreichardt authored Feb 23, 2023
1 parent 3fd0840 commit 29c0cad
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 193 deletions.
14 changes: 0 additions & 14 deletions lib/util/lib/src/decoding_encoding_helper.dart

This file was deleted.

99 changes: 0 additions & 99 deletions lib/util/lib/src/encryption/rsa_pem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,6 @@ import 'package:rsa_pkcs/rsa_pkcs.dart' as pkcs;

import 'rsa_encryption.dart' as rsa_encryptable;

List<int> decodePEM(String pem) {
var startsWith = [
"-----BEGIN PUBLIC KEY-----",
"-----BEGIN PRIVATE KEY-----",
"-----BEGIN RSA PUBLIC KEY-----",
"-----BEGIN RSA PRIVATE KEY-----",
"-----BEGIN PGP PUBLIC KEY BLOCK-----\r\nVersion: React-Native-OpenPGP.js 0.1\r\nComment: http://openpgpjs.org\r\n\r\n",
"-----BEGIN PGP PRIVATE KEY BLOCK-----\r\nVersion: React-Native-OpenPGP.js 0.1\r\nComment: http://openpgpjs.org\r\n\r\n",
];
var endsWith = [
"-----END PUBLIC KEY-----",
"-----END PRIVATE KEY-----",
"-----END RSA PUBLIC KEY-----",
"-----END RSA PRIVATE KEY-----",
"-----END PGP PUBLIC KEY BLOCK-----",
"-----END PGP PRIVATE KEY BLOCK-----",
];
bool isOpenPgp = pem.indexOf('BEGIN PGP') != -1;

for (var s in startsWith) {
if (pem.startsWith(s)) {
pem = pem.substring(s.length);
}
}

for (var s in endsWith) {
if (pem.endsWith(s)) {
pem = pem.substring(0, pem.length - s.length);
}
}

if (isOpenPgp) {
var index = pem.indexOf('\r\n');
pem = pem.substring(0, index);
}

pem = pem.replaceAll('\n', '');
pem = pem.replaceAll('\r', '');

return base64.decode(pem);
}

class RsaKeyHelper {
AsymmetricKeyPair<PublicKey, PrivateKey> generateKeyPair() {
var keyParams =
Expand Down Expand Up @@ -99,16 +57,6 @@ class RsaKeyHelper {
final parser = pkcs.RSAPKCSParser();
final parserResult = parser.parsePEM(privateKey);
final private = parserResult.private!;
/*
print("modulus: ${private.modulus}");
print("publicExponent: ${private.publicExponent}");
print("privateExponent: ${private.privateExponent}");
print("exponent1: ${private.exponent1}");
print("exponent2: ${private.exponent2}");
print("Prime1: ${private.prime1}");
print("Prime2: ${private.prime2}");
print("coefficient: ${private.coefficient}");
*/

final privateKeyObject = RSAPrivateKey(
private.modulus,
Expand All @@ -124,53 +72,6 @@ class RsaKeyHelper {
privateKeyObject, publicKeyObject);
}

parsePublicKeyFromPem(pemString) {
List<int> publicKeyDER = decodePEM(pemString);
var asn1Parser = new ASN1Parser(publicKeyDER as Uint8List);
var topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
var publicKeyBitString = topLevelSeq.elements[1];

var publicKeyAsn = new ASN1Parser(publicKeyBitString.contentBytes()!);
ASN1Sequence publicKeySeq = publicKeyAsn.nextObject() as ASN1Sequence;
var modulus = publicKeySeq.elements[0] as ASN1Integer;
var exponent = publicKeySeq.elements[1] as ASN1Integer;

RSAPublicKey rsaPublicKey =
RSAPublicKey(modulus.valueAsBigInteger!, exponent.valueAsBigInteger!);

return rsaPublicKey;
}

parsePrivateKeyFromPem(pemString) {
List<int> privateKeyDER = decodePEM(pemString);
var asn1Parser = new ASN1Parser(privateKeyDER as Uint8List);
var topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
// var version = topLevelSeq.elements[0];
// var algorithm = topLevelSeq.elements[1];
var privateKey = topLevelSeq.elements[2];

asn1Parser = new ASN1Parser(privateKey.contentBytes()!);
var pkSeq = asn1Parser.nextObject() as ASN1Sequence;

// version = pkSeq.elements[0];
var modulus = pkSeq.elements[1] as ASN1Integer;
// var publicExponent = pkSeq.elements[2] as ASN1Integer;
var privateExponent = pkSeq.elements[3] as ASN1Integer;
var p = pkSeq.elements[4] as ASN1Integer;
var q = pkSeq.elements[5] as ASN1Integer;
// var exp1 = pkSeq.elements[6] as ASN1Integer;
// var exp2 = pkSeq.elements[7] as ASN1Integer;
// var co = pkSeq.elements[8] as ASN1Integer;

RSAPrivateKey rsaPrivateKey = RSAPrivateKey(
modulus.valueAsBigInteger!,
privateExponent.valueAsBigInteger!,
p.valueAsBigInteger,
q.valueAsBigInteger);

return rsaPrivateKey;
}

encodePublicKeyToPem(RSAPublicKey publicKey) {
var algorithmSeq = new ASN1Sequence();
var algorithmAsn1Obj = new ASN1Object.fromBytes(Uint8List.fromList(
Expand Down
72 changes: 0 additions & 72 deletions lib/util/lib/src/logging.dart

This file was deleted.

6 changes: 0 additions & 6 deletions lib/util/lib/src/string_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
//
// SPDX-License-Identifier: EUPL-1.2

bool isNotEmptyOrNull(String? value) => !isEmptyOrNull(value);

bool isEmptyOrNull(String? value) {
return value == null || value.isEmpty;
}

void throwIfNullOrEmpty(String string, [String? name]) {
ArgumentError.checkNotNull(string, name);
if (string.isEmpty) {
Expand Down
2 changes: 0 additions & 2 deletions lib/util/lib/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@

library util;

export 'src/decoding_encoding_helper.dart';
export 'src/string_helper.dart';
export 'src/logging.dart';

0 comments on commit 29c0cad

Please sign in to comment.