Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Dec 11, 2024
1 parent 96f38aa commit ee80899
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 60 deletions.
2 changes: 1 addition & 1 deletion lib/src/message/literal_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
[
CompressedDataPacket.fromPacketList(
_unwrapCompressed(),
algorithm: algorithm,
algorithm,
)
],
),
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/aead_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class AeadEncryptedDataPacket extends BasePacket implements EncryptedDataPacketI
final Uint8List data, {
final Uint8List? finalChunk,
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
AeadAlgorithm aead = AeadAlgorithm.gcm,
final AeadAlgorithm aead = AeadAlgorithm.ocb,
final chunkSizeByte = 0,
final Uint8List? iv,
}) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/compressed_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class CompressedDataPacket extends BasePacket {
}

factory CompressedDataPacket.fromPacketList(
final PacketListInterface packets, {
final PacketListInterface packets, [
final CompressionAlgorithm algorithm = CompressionAlgorithm.uncompressed,
}) =>
]) =>
CompressedDataPacket(
_compress(packets, algorithm),
packets,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/image_user_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class ImageUserAttribute extends UserAttributeSubpacket {
}) : super(jpeg, data);

factory ImageUserAttribute.fromBytes(
final Uint8List imageData, {
final Uint8List imageData, [
final int imageType = jpeg,
}) {
]) {
return ImageUserAttribute(Uint8List.fromList([
0x10,
0x00,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/literal_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class LiteralDataPacket extends BasePacket implements LiteralDataInterface {
}

factory LiteralDataPacket.fromText(
final String text, {
final String text, [
final DateTime? time,
}) =>
]) =>
LiteralDataPacket(
Uint8List(0),
text: text,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/packet/one_pass_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class OnePassSignaturePacket extends BasePacket {
}
}

factory OnePassSignaturePacket.fromBytes(final Uint8List bytes) {
factory OnePassSignaturePacket.fromBytes(
final Uint8List bytes,
) {
var pos = 0;

/// A one-octet version number (4 or 6).
Expand Down
6 changes: 3 additions & 3 deletions lib/src/packet/packet_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ class PacketList extends ListBase<PacketInterface> implements PacketListInterfac
get length => packets.length;

@override
operator [](int index) => packets[index];
operator [](final int index) => packets[index];

@override
operator []=(int index, PacketInterface packet) {
operator []=(final int index, PacketInterface packet) {
packets[index] = packet;
}

@override
set length(int newLength) {
set length(final int newLength) {
packets.length = newLength;
}
}
5 changes: 4 additions & 1 deletion lib/src/packet/packet_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ final class PacketReader {

PacketReader(this.type, this.data, this.offset);

factory PacketReader.read(final Uint8List bytes, [final int offset = 0]) {
factory PacketReader.read(
final Uint8List bytes, [
final int offset = 0,
]) {
if (bytes.length <= offset || bytes.sublist(offset).length < 2 || (bytes[offset] & 0x80) == 0) {
throw ArgumentError(
'Error during parsing.'
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/padding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PaddingPacket extends BasePacket {

PaddingPacket(this.padding) : super(PacketType.marker);

factory PaddingPacket.createPadding(int lengh) {
factory PaddingPacket.createPadding(final int lengh) {
assert(paddingMin <= lengh && lengh <= paddingMax);
return PaddingPacket(Helper.randomBytes(lengh));
}
Expand Down
12 changes: 7 additions & 5 deletions lib/src/packet/secret_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ class SecretKeyPacket extends BasePacket implements SecretKeyPacketInterface {
/// Generate secret key packet
factory SecretKeyPacket.generate(
final KeyAlgorithm algorithm, {
final RSAKeySize rsaKeySize = RSAKeySize.normal,
final Ecc curve = Ecc.secp521r1,
final RSAKeySize rsaKeySize = RSAKeySize.normal,
final DateTime? time,
}) {
final keyMaterial = generateKeyMaterial(
algorithm,
rsaKeySize: rsaKeySize,
curve: curve,
rsaKeySize: rsaKeySize,
);

return SecretKeyPacket(
Expand All @@ -103,8 +103,8 @@ class SecretKeyPacket extends BasePacket implements SecretKeyPacketInterface {

static SecretKeyMaterialInterface generateKeyMaterial(
final KeyAlgorithm algorithm, {
final RSAKeySize rsaKeySize = RSAKeySize.normal,
final Ecc curve = Ecc.secp521r1,
final RSAKeySize rsaKeySize = RSAKeySize.normal,
}) {
return switch (algorithm) {
KeyAlgorithm.rsaEncryptSign ||
Expand Down Expand Up @@ -353,7 +353,9 @@ class SecretKeyPacket extends BasePacket implements SecretKeyPacketInterface {
);
}

SecretKeyMaterialInterface decryptKeyData(final String passphrase) {
SecretKeyMaterialInterface decryptKeyData(
final String passphrase,
) {
final Uint8List clearText;
if (isEncrypted) {
final kek = _produceEncryptionKey(
Expand Down Expand Up @@ -566,7 +568,7 @@ class SecretKeyPacket extends BasePacket implements SecretKeyPacketInterface {
return keyMaterial;
}

static Uint8List _computeChecksum(Uint8List keyData) {
static Uint8List _computeChecksum(final Uint8List keyData) {
var sum = 0;
for (var i = 0; i < keyData.length; i++) {
sum = (sum + keyData[i]) & 0xffff;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/secret_subkey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class SecretSubkeyPacket extends SecretKeyPacket implements SubkeyPacketInterfac
/// Generate secret subkey packet
factory SecretSubkeyPacket.generate(
final KeyAlgorithm algorithm, {
final RSAKeySize rsaKeySize = RSAKeySize.normal,
final Ecc curve = Ecc.secp521r1,
final RSAKeySize rsaKeySize = RSAKeySize.normal,
final DateTime? time,
}) {
final keyMaterial = SecretKeyPacket.generateKeyMaterial(
algorithm,
rsaKeySize: rsaKeySize,
curve: curve,
rsaKeySize: rsaKeySize,
);
return SecretSubkeyPacket(
PublicSubkeyPacket(
Expand Down
52 changes: 28 additions & 24 deletions lib/src/packet/signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {

/// Create signature
factory SignaturePacket.createSignature(
SecretKeyPacketInterface signKey,
SignatureType signatureType,
Uint8List dataToSign, {
final SecretKeyPacketInterface signKey,
final SignatureType signatureType,
final Uint8List dataToSign, {
final HashAlgorithm? preferredHash,
final Iterable<SubpacketInterface> subpackets = const [],
final DateTime? time,
Expand Down Expand Up @@ -227,9 +227,9 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {

/// Create direct key signature
factory SignaturePacket.createDirectKeySignature(
SecretKeyPacketInterface signKey, {
int keyExpiry = 0,
DateTime? time,
final SecretKeyPacketInterface signKey, {
final int keyExpiry = 0,
final DateTime? time,
}) {
final subpackets = _keySubpackets(signKey.keyVersion);
if (keyExpiry > 0) {
Expand All @@ -246,11 +246,11 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {

/// Create self signature
factory SignaturePacket.createSelfCertificate(
SecretKeyPacketInterface signKey,
UserIDPacketInterface userID, {
bool isPrimaryUser = false,
int keyExpiry = 0,
DateTime? time,
final SecretKeyPacketInterface signKey,
final UserIDPacketInterface userID, {
final bool isPrimaryUser = false,
final int keyExpiry = 0,
final DateTime? time,
}) {
final subpackets = signKey.keyVersion == KeyVersion.v4.value
? _keySubpackets(
Expand All @@ -277,11 +277,11 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {

/// Create subkey binding signature
factory SignaturePacket.createSubkeyBinding(
SecretKeyPacketInterface signKey,
SubkeyPacketInterface subkey, {
int keyExpiry = 0,
bool forSigning = false,
DateTime? time,
final SecretKeyPacketInterface signKey,
final SubkeyPacketInterface subkey, {
final int keyExpiry = 0,
final bool forSigning = false,
final DateTime? time,
}) {
final subpackets = <SubpacketInterface>[];
if (keyExpiry > 0) {
Expand Down Expand Up @@ -325,11 +325,11 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {

/// Create literal data signature
factory SignaturePacket.createLiteralData(
SecretKeyPacketInterface signKey,
LiteralDataInterface literalData, {
Iterable<KeyInterface> recipients = const [],
NotationDataInterface? notationData,
DateTime? time,
final SecretKeyPacketInterface signKey,
final LiteralDataInterface literalData, {
final Iterable<KeyInterface> recipients = const [],
final NotationDataInterface? notationData,
final DateTime? time,
}) {
final signatureType = switch (literalData.format) {
LiteralFormat.text || LiteralFormat.utf8 => SignatureType.text,
Expand Down Expand Up @@ -485,7 +485,9 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {
}
}

static List<SubpacketInterface> _keySubpackets(final int version) {
static List<SubpacketInterface> _keySubpackets(
final int version,
) {
final subpackets = [
KeyFlags.fromFlags(
KeyFlag.certifyKeys.value | KeyFlag.signData.value,
Expand Down Expand Up @@ -550,7 +552,9 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {
}
}

static List<SubpacketInterface> _readSubpackets(final Uint8List bytes) {
static List<SubpacketInterface> _readSubpackets(
final Uint8List bytes,
) {
final subpackets = <SubpacketInterface>[];
var offset = 0;
while (offset < bytes.length) {
Expand Down Expand Up @@ -775,7 +779,7 @@ class SignaturePacket extends BasePacket implements SignaturePacketInterface {
/// Encode subpacket to bytes
static Uint8List _encodeSubpackets(
final Iterable<SubpacketInterface> subpackets,
bool isV6,
final bool isV6,
) {
final bytes = subpackets
.map(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/packet/sym_encrypted_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ class SymEncryptedDataPacket extends BasePacket implements EncryptedDataPacketIn

factory SymEncryptedDataPacket.encryptPackets(
final Uint8List key,
final PacketListInterface packets, {
final PacketListInterface packets, [
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
}) {
]) {
final cipher = BufferedCipher(symmetric.cfbCipherEngine)
..init(
true,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/packet/sym_encrypted_integrity_protected_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class SymEncryptedIntegrityProtectedDataPacket extends BasePacket implements Enc
final Uint8List data, {
final Uint8List? finalChunk,
final SymmetricAlgorithm symmetric = SymmetricAlgorithm.aes128,
final AeadAlgorithm aead = AeadAlgorithm.gcm,
final AeadAlgorithm aead = AeadAlgorithm.ocb,
final chunkSizeByte = 0,
final Uint8List? salt,
}) {
Expand Down
6 changes: 4 additions & 2 deletions lib/src/packet/sym_encrypted_session_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ class SymEncryptedSessionKeyPacket extends BasePacket {
}
}

factory SymEncryptedSessionKeyPacket.fromBytes(final Uint8List bytes) {
factory SymEncryptedSessionKeyPacket.fromBytes(
final Uint8List bytes,
) {
var pos = 0;

/// A one-octet version number.
Expand Down Expand Up @@ -185,7 +187,7 @@ class SymEncryptedSessionKeyPacket extends BasePacket {
iv,
encrypted,
symmetric: symmetric,
aead: aeadProtect ? aead : null,
aead: aead,
sessionKey: sessionKey ?? SessionKey(key, symmetric),
);
}
Expand Down
7 changes: 4 additions & 3 deletions lib/src/packet/trust.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class TrustPacket extends BasePacket {
Uint8List.fromList([trustCode & 0xff]),
);

factory TrustPacket.fromBytes(final Uint8List bytes) => TrustPacket.fromTrustCode(
bytes[0],
);
factory TrustPacket.fromBytes(
final Uint8List bytes,
) =>
TrustPacket.fromTrustCode(bytes[0]);

@override
get data => levelAndTrustAmount;
Expand Down
4 changes: 1 addition & 3 deletions lib/src/packet/user_attribute.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ class UserAttributePacket extends BasePacket implements UserIDPacketInterface {
factory UserAttributePacket.fromBytes(
final Uint8List bytes,
) =>
UserAttributePacket(
_readSubpackets(bytes),
);
UserAttributePacket(_readSubpackets(bytes));

@override
get data => Uint8List.fromList(
Expand Down
6 changes: 3 additions & 3 deletions test/packet/compression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main() {
test('zip test', () {
final compressedPacket = CompressedDataPacket.fromPacketList(
PacketList([literalData]),
algorithm: CompressionAlgorithm.zip,
CompressionAlgorithm.zip,
);

final decompressedPacket = CompressedDataPacket.fromBytes(
Expand All @@ -36,7 +36,7 @@ void main() {
test('zlib test', () {
final compressedPacket = CompressedDataPacket.fromPacketList(
PacketList([literalData]),
algorithm: CompressionAlgorithm.zlib,
CompressionAlgorithm.zlib,
);

final decompressedPacket = CompressedDataPacket.fromBytes(
Expand All @@ -61,7 +61,7 @@ void main() {
expect(
() => CompressedDataPacket.fromPacketList(
PacketList([literalData]),
algorithm: CompressionAlgorithm.bzip2,
CompressionAlgorithm.bzip2,
),
throwsUnsupportedError,
);
Expand Down
2 changes: 1 addition & 1 deletion test/packet/encryption_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void main() {
final encrypted = SymEncryptedDataPacket.encryptPackets(
key,
packets,
symmetric: SymmetricAlgorithm.aes128,
SymmetricAlgorithm.aes128,
);
final encrypt = SymEncryptedDataPacket.fromBytes(
encrypted.data,
Expand Down

0 comments on commit ee80899

Please sign in to comment.