Skip to content

Commit

Permalink
format code by Dart formatter
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Mar 23, 2023
1 parent ede055d commit 987cbf7
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 72 deletions.
9 changes: 6 additions & 3 deletions lib/src/armor/armor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Armor {
static const endOfLine = '-----\n';

static const splitPattern = r'^-----[^-]+-----$';
static const emptyLinePattern = r'^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$';
static const emptyLinePattern =
r'^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$';
static const headerPattern = r'^([^\s:]|[^\s:][^:]*[^\s:]): .+$';
static const beginPattern =
r'^-----BEGIN PGP (MESSAGE, PART \d+\/\d+|MESSAGE, PART \d+|SIGNED MESSAGE|MESSAGE|PUBLIC KEY BLOCK|PRIVATE KEY BLOCK|SIGNATURE)-----$';
Expand Down Expand Up @@ -92,11 +93,13 @@ class Armor {
final text = textLines.join('\r\n').trim();
final data = base64.decode(dataLines.join().trim());

if ((checksum != _crc24Checksum(data)) && (checksum.isNotEmpty || checksumRequired)) {
if ((checksum != _crc24Checksum(data)) &&
(checksum.isNotEmpty || checksumRequired)) {
throw StateError('Ascii armor integrity check failed');
}

return Armor(type ?? ArmorType.multipartSection, data, headers: headers, text: text);
return Armor(type ?? ArmorType.multipartSection, data,
headers: headers, text: text);
}

/// Armor an OpenPGP binary packet block
Expand Down
3 changes: 2 additions & 1 deletion lib/src/packet/image_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class ImageAttributeSubpacket extends UserAttributeSubpacket {

static final Uint8List _zeroes = Uint8List(12);

ImageAttributeSubpacket(final Uint8List data, {super.isLongLength}) : super(1, data);
ImageAttributeSubpacket(final Uint8List data, {super.isLongLength})
: super(1, data);

factory ImageAttributeSubpacket.fromImageData(
final Uint8List imageData, {
Expand Down
15 changes: 11 additions & 4 deletions lib/src/packet/key/dsa_secret_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class DSASecretParams extends KeyParams {

DSASecretParams(this.secretExponent);

factory DSASecretParams.fromByteData(final Uint8List bytes) => DSASecretParams(
factory DSASecretParams.fromByteData(final Uint8List bytes) =>
DSASecretParams(
Helper.readMPI(bytes),
);

Expand Down Expand Up @@ -62,7 +63,10 @@ class DSASecretParams extends KeyParams {

// g has order q
// Check that g ** q = 1 mod p
if (publicParams.generator.modPow(publicParams.order, publicParams.prime).compareTo(BigInt.one) != 0) {
if (publicParams.generator
.modPow(publicParams.order, publicParams.prime)
.compareTo(BigInt.one) !=
0) {
return false;
}

Expand All @@ -75,8 +79,11 @@ class DSASecretParams extends KeyParams {
// Re-derive public key y' = g ** x mod p
// Expect y == y'
// Blinded exponentiation computes g**{rq + x} to compare to y
final r = Helper.randomBigIntInRange(BigInt.two << (qSize - 1), BigInt.two << qSize);
final r = Helper.randomBigIntInRange(
BigInt.two << (qSize - 1), BigInt.two << qSize);
final rqx = (publicParams.order * r) + secretExponent;
return publicParams.publicExponent.compareTo(publicParams.generator.modPow(rqx, publicParams.prime)) == 0;
return publicParams.publicExponent.compareTo(
publicParams.generator.modPow(rqx, publicParams.prime)) ==
0;
}
}
6 changes: 4 additions & 2 deletions lib/src/packet/key/ec_secret_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ class ECSecretParams extends KeyParams {
case CurveInfo.ed25519:
return false;
default:
final parameters = ECDomainParameters(publicParams.curve.name.toLowerCase());
final q = parameters.curve.decodePoint(publicParams.q.toUnsignedBytes());
final parameters =
ECDomainParameters(publicParams.curve.name.toLowerCase());
final q =
parameters.curve.decodePoint(publicParams.q.toUnsignedBytes());
return q != null && !q.isInfinity && (parameters.G * d) == q;
}
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/packet/key/ecdh_public_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ class ECDHPublicParams extends ECPublicParams {

final kdfBytes = bytes.sublist(pos);
final reserved = kdfBytes[1];
final kdfHash = HashAlgorithm.values.firstWhere((hash) => hash.value == kdfBytes[2]);
final kdfSymmetric = SymmetricAlgorithm.values.firstWhere((sym) => sym.value == kdfBytes[3]);
final kdfHash =
HashAlgorithm.values.firstWhere((hash) => hash.value == kdfBytes[2]);
final kdfSymmetric =
SymmetricAlgorithm.values.firstWhere((sym) => sym.value == kdfBytes[3]);
return ECDHPublicParams(
oid,
q,
Expand Down
12 changes: 8 additions & 4 deletions lib/src/packet/key/ecdh_session_key_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class ECDHSessionKeyParams extends SessionKeyParams {
'Curve ${publicParams.curve.name} is unsupported for ephemeral key generation.',
);
default:
final parameters = ECDomainParameters(publicParams.curve.name.toLowerCase());
final parameters =
ECDomainParameters(publicParams.curve.name.toLowerCase());
final keyGen = KeyGenerator('EC')
..init(
ParametersWithRandom(
Expand Down Expand Up @@ -136,7 +137,8 @@ class ECDHSessionKeyParams extends SessionKeyParams {
case CurveInfo.curve25519:
sharedKey = TweetNaCl.crypto_scalarmult(
Uint8List(TweetNaCl.sharedKeyLength),
Uint8List.fromList(secretParams.d.toUnsignedBytes().reversed.toList()),
Uint8List.fromList(
secretParams.d.toUnsignedBytes().reversed.toList()),
ephemeralKey.toUnsignedBytes(),
);
break;
Expand All @@ -145,7 +147,8 @@ class ECDHSessionKeyParams extends SessionKeyParams {
'Curve ${publicParams.curve.name} is unsupported for key agreement calculation.',
);
default:
final parameters = ECDomainParameters(publicParams.curve.name.toLowerCase());
final parameters =
ECDomainParameters(publicParams.curve.name.toLowerCase());
final privateKey = ECPrivateKey(secretParams.d, parameters);
final agreement = ECDHBasicAgreement()..init(privateKey);
sharedKey = agreement
Expand Down Expand Up @@ -209,7 +212,8 @@ class ECDHSessionKeyParams extends SessionKeyParams {
/// Add pkcs5 padding to a message
static Uint8List _pkcs5Encode(final Uint8List message) {
final c = 8 - (message.lengthInBytes % 8);
return Uint8List.fromList(List.filled(message.length + c, c))..setAll(0, message);
return Uint8List.fromList(List.filled(message.length + c, c))
..setAll(0, message);
}

/// Remove pkcs5 padding from a message
Expand Down
15 changes: 11 additions & 4 deletions lib/src/packet/key/elgamal_secret_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ElGamalSecretParams implements KeyParams {

ElGamalSecretParams(this.secretExponent);

factory ElGamalSecretParams.fromByteData(final Uint8List bytes) => ElGamalSecretParams(
factory ElGamalSecretParams.fromByteData(final Uint8List bytes) =>
ElGamalSecretParams(
Helper.readMPI(bytes),
);

Expand All @@ -41,7 +42,10 @@ class ElGamalSecretParams implements KeyParams {

// g should have order p-1
// Check that g ** (p-1) = 1 mod p
if (publicParams.generator.modPow(publicParams.prime - BigInt.one, publicParams.prime).compareTo(BigInt.one) != 0) {
if (publicParams.generator
.modPow(publicParams.prime - BigInt.one, publicParams.prime)
.compareTo(BigInt.one) !=
0) {
return false;
}

Expand All @@ -62,8 +66,11 @@ class ElGamalSecretParams implements KeyParams {
// Re-derive public key y' = g ** x mod p
// Expect y == y'
// Blinded exponentiation computes g**{r(p-1) + x} to compare to y
final r = Helper.randomBigIntInRange(BigInt.two << (pSize - 1), BigInt.two << pSize);
final r = Helper.randomBigIntInRange(
BigInt.two << (pSize - 1), BigInt.two << pSize);
final rqx = ((publicParams.prime - BigInt.one) * r) + secretExponent;
return publicParams.publicExponent.compareTo(publicParams.generator.modPow(rqx, publicParams.prime)) == 0;
return publicParams.publicExponent.compareTo(
publicParams.generator.modPow(rqx, publicParams.prime)) ==
0;
}
}
3 changes: 2 additions & 1 deletion lib/src/packet/key/rsa_secret_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class RSASecretParams extends KeyParams {
final nSizeOver3 = (publicParams.modulus.bitLength / 3).floor();
final r = Helper.randomBigIntInRange(BigInt.two, BigInt.two << nSizeOver3);
final rde = r * privateExponent * publicParams.publicExponent;
return (rde % (primeP - BigInt.one)).compareTo(r) == 0 && (rde % (primeQ - BigInt.one)).compareTo(r) == 0;
return (rde % (primeP - BigInt.one)).compareTo(r) == 0 &&
(rde % (primeQ - BigInt.one)).compareTo(r) == 0;
}
}
4 changes: 2 additions & 2 deletions lib/src/packet/key/rsa_session_key_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class RSASessionKeyParams extends SessionKeyParams {
final AsymmetricBlockCipher engine,
final Uint8List input,
) {
final numBlocks =
input.length ~/ engine.inputBlockSize + ((input.lengthInBytes % engine.inputBlockSize != 0) ? 1 : 0);
final numBlocks = input.length ~/ engine.inputBlockSize +
((input.lengthInBytes % engine.inputBlockSize != 0) ? 1 : 0);

final output = Uint8List(numBlocks * engine.outputBlockSize);

Expand Down
3 changes: 2 additions & 1 deletion lib/src/packet/key/s2k.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class S2K {
var itCount = _defaultItCount;
final type = S2kType.values.firstWhere((type) => type.value == bytes[pos]);
pos++;
final hash = HashAlgorithm.values.firstWhere((hash) => hash.value == bytes[pos]);
final hash =
HashAlgorithm.values.firstWhere((hash) => hash.value == bytes[pos]);
pos++;

final Uint8List salt;
Expand Down
9 changes: 6 additions & 3 deletions lib/src/packet/key/session_key_params.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ abstract class SessionKeyParams {
Uint8List encode();

SessionKey decodeSessionKey(final Uint8List data) {
final sessionKeySymmetric = SymmetricAlgorithm.values.firstWhere((algo) => algo.value == data[0]);
final sessionKey = SessionKey(data.sublist(1, data.length - 2), sessionKeySymmetric);
final sessionKeySymmetric =
SymmetricAlgorithm.values.firstWhere((algo) => algo.value == data[0]);
final sessionKey =
SessionKey(data.sublist(1, data.length - 2), sessionKeySymmetric);
final checksum = data.sublist(data.length - 2);
final computedChecksum = sessionKey.computeChecksum();
final isValidChecksum = (computedChecksum[0] == checksum[0]) && (computedChecksum[1] == checksum[1]);
final isValidChecksum = (computedChecksum[0] == checksum[0]) &&
(computedChecksum[1] == checksum[1]);
if (!isValidChecksum) {
throw StateError('Session key decryption error');
}
Expand Down
13 changes: 10 additions & 3 deletions lib/src/packet/literal_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class LiteralDataPacket extends ContainedPacket {

factory LiteralDataPacket.fromByteData(final Uint8List bytes) {
var pos = 0;
final format = LiteralFormat.values.firstWhere((format) => format.value == bytes[pos]);
final format =
LiteralFormat.values.firstWhere((format) => format.value == bytes[pos]);
pos++;
final length = bytes[pos++];
final filename = utf8.decode(bytes.sublist(pos, pos + length));
Expand All @@ -47,7 +48,9 @@ class LiteralDataPacket extends ContainedPacket {

pos += 4;
final data = bytes.sublist(pos);
final text = (format == LiteralFormat.text || format == LiteralFormat.utf8) ? utf8.decode(data) : '';
final text = (format == LiteralFormat.text || format == LiteralFormat.utf8)
? utf8.decode(data)
: '';

return LiteralDataPacket(
data,
Expand Down Expand Up @@ -87,6 +90,10 @@ class LiteralDataPacket extends ContainedPacket {
}

Uint8List writeForSign() {
return data.isNotEmpty ? data : text.replaceAll(RegExp(r'\r?\n', multiLine: true), '\r\n').stringToBytes();
return data.isNotEmpty
? data
: text
.replaceAll(RegExp(r'\r?\n', multiLine: true), '\r\n')
.stringToBytes();
}
}
3 changes: 2 additions & 1 deletion lib/src/packet/modification_detection_code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import 'contained_packet.dart';
class ModificationDetectionCodePacket extends ContainedPacket {
final Uint8List digest;

ModificationDetectionCodePacket(this.digest) : super(PacketTag.modificationDetectionCode);
ModificationDetectionCodePacket(this.digest)
: super(PacketTag.modificationDetectionCode);

factory ModificationDetectionCodePacket.fromByteData(final Uint8List bytes) =>
ModificationDetectionCodePacket(bytes);
Expand Down
11 changes: 8 additions & 3 deletions lib/src/packet/packet_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import 'user_id.dart';
class PacketList extends ListBase<ContainedPacket> {
final List<ContainedPacket> packets;

PacketList(final Iterable<ContainedPacket> packets) : packets = packets.toList(growable: false);
PacketList(final Iterable<ContainedPacket> packets)
: packets = packets.toList(growable: false);

factory PacketList.packetDecode(final Uint8List bytes) {
final packets = <ContainedPacket>[];
Expand Down Expand Up @@ -93,15 +94,19 @@ class PacketList extends ListBase<ContainedPacket> {
);
break;
case PacketTag.modificationDetectionCode:
packets.add(ModificationDetectionCodePacket.fromByteData(reader.data));
packets
.add(ModificationDetectionCodePacket.fromByteData(reader.data));
break;
}
}
return PacketList(packets);
}

Uint8List encode() => Uint8List.fromList(
packets.map((packet) => packet.encode()).expand((byte) => byte).toList(growable: false),
packets
.map((packet) => packet.encode())
.expand((byte) => byte)
.toList(growable: false),
);

PacketList filterByTags([final List<PacketTag> tags = const []]) {
Expand Down
11 changes: 8 additions & 3 deletions lib/src/packet/packet_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class PacketReader {
PacketReader(this.tag, this.data, this.start, this.end);

factory PacketReader.read(final Uint8List bytes, [final int start = 0]) {
if (bytes.length <= start || bytes.sublist(start).length < 2 || (bytes[start] & 0x80) == 0) {
if (bytes.length <= start ||
bytes.sublist(start).length < 2 ||
(bytes[start] & 0x80) == 0) {
throw StateError(
'Error during parsing. This data probably does not conform to a valid OpenPGP format.',
);
Expand Down Expand Up @@ -66,7 +68,9 @@ class PacketReader {
partialPos += partialLen;
break;
} else if (bytes[partialPos] >= 192 && bytes[partialPos] < 224) {
final partialLen = ((bytes[partialPos++] - 192) << 8) + (bytes[partialPos++]) + 192;
final partialLen = ((bytes[partialPos++] - 192) << 8) +
(bytes[partialPos++]) +
192;
packetLength += partialLen;
partialPos += partialLen;
break;
Expand All @@ -78,7 +82,8 @@ class PacketReader {
} else {
partialPos++;

final partialLen = bytes.sublist(partialPos, partialPos + 4).toInt32();
final partialLen =
bytes.sublist(partialPos, partialPos + 4).toInt32();
partialPos += 4;

packetLength += partialLen;
Expand Down
9 changes: 6 additions & 3 deletions lib/src/packet/public_key_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class PublicKeyEncryptedSessionKeyPacket extends ContainedPacket {
this.sessionKey,
}) : super(PacketTag.publicKeyEncryptedSessionKey);

factory PublicKeyEncryptedSessionKeyPacket.fromByteData(final Uint8List bytes) {
factory PublicKeyEncryptedSessionKeyPacket.fromByteData(
final Uint8List bytes) {
var pos = 0;
final pkeskVersion = bytes[pos++];
if (pkeskVersion != version) {
Expand All @@ -62,7 +63,8 @@ class PublicKeyEncryptedSessionKeyPacket extends ContainedPacket {
final keyID = bytes.sublist(pos, pos + 8);
pos += 8;

final keyAlgorithm = KeyAlgorithm.values.firstWhere((algo) => algo.value == bytes[pos]);
final keyAlgorithm =
KeyAlgorithm.values.firstWhere((algo) => algo.value == bytes[pos]);
pos++;

final SessionKeyParams params;
Expand Down Expand Up @@ -140,7 +142,8 @@ class PublicKeyEncryptedSessionKeyPacket extends ContainedPacket {
]);
}

Future<PublicKeyEncryptedSessionKeyPacket> decrypt(final SecretKeyPacket key) async {
Future<PublicKeyEncryptedSessionKeyPacket> decrypt(
final SecretKeyPacket key) async {
if (isDecrypted) {
return this;
} else {
Expand Down
9 changes: 6 additions & 3 deletions lib/src/packet/secret_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
}

Uint8List? iv;
if (!(s2k != null && s2k.type == S2kType.gnu) && s2kUsage != S2kUsage.none) {
if (!(s2k != null && s2k.type == S2kType.gnu) &&
s2kUsage != S2kUsage.none) {
final blockSize = symmetric.blockSize;
iv = bytes.sublist(pos, pos + blockSize);
pos += blockSize;
Expand Down Expand Up @@ -237,7 +238,8 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
if (secretParams == null) {
final Uint8List clearText;
if (isEncrypted) {
final key = await s2k?.produceKey(passphrase, symmetric) ?? Uint8List((symmetric.keySize + 7) >> 3);
final key = await s2k?.produceKey(passphrase, symmetric) ??
Uint8List((symmetric.keySize + 7) >> 3);
final cipher = BufferedCipher(symmetric.cipherEngine)
..init(
false,
Expand Down Expand Up @@ -291,7 +293,8 @@ class SecretKeyPacket extends ContainedPacket implements KeyPacket {
return keyParams.validatePublicParams(publicParams as DSAPublicParams);
}
if (keyParams is ElGamalSecretParams) {
return keyParams.validatePublicParams(publicParams as ElGamalPublicParams);
return keyParams
.validatePublicParams(publicParams as ElGamalPublicParams);
}
if (keyParams is ECSecretParams) {
return keyParams.validatePublicParams(publicParams as ECPublicParams);
Expand Down
Loading

0 comments on commit 987cbf7

Please sign in to comment.