Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:replace md5 checksum #511

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/at_commons/lib/at_commons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ export 'package:at_commons/src/enroll/enrollment.dart';
@experimental
export 'package:at_commons/src/telemetry/at_telemetry.dart';
export 'package:at_commons/src/utils/string_utils.dart';
export 'package:at_commons/src/keystore/public_key_hash.dart';
3 changes: 3 additions & 0 deletions packages/at_commons/lib/src/at_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class AtConstants {
static const String sharedKeyStatus = 'sharedKeyStatus';
static const String sharedKeyEncrypted = 'sharedKeyEnc';
static const String sharedWithPublicKeyCheckSum = 'pubKeyCS';
static const String sharedWithPublicKeyHash = 'pubKeyHash';
static const String sharedWithPublicKeyHashValue = 'hash';
static const String sharedWithPublicKeyHashAlgo = 'algo';
static const String sharedKeyEncryptedEncryptingKeyName = 'skeEncKeyName';
static const String sharedKeyEncryptedEncryptingAlgo = 'skeEncAlgo';
static const String firstByte = '#';
Expand Down
22 changes: 21 additions & 1 deletion packages/at_commons/lib/src/keystore/at_key.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:at_commons/src/keystore/at_key_builder_impl.dart';
import 'package:at_commons/src/utils/at_key_regex_utils.dart';
import 'package:at_commons/src/utils/string_utils.dart';
import 'package:at_commons/src/keystore/public_key_hash.dart';
import 'package:meta/meta.dart';

import '../at_constants.dart';
Expand Down Expand Up @@ -489,8 +490,13 @@ class Metadata {

/// Stores the checksum of the encryption public key used to encrypt the [sharedKeyEnc]. We use this
/// to verify that the encryption key-pair used to encrypt and decrypt the value are same
@Deprecated('Use pubKeyHash')
String? pubKeyCS;

/// Stores the hash of the encryption public key used to encrypt the [sharedKeyEnc]
/// The hash is used to verify whether the current atsign's public key used for encrypting data by another atsign, has changed while decrypting the data
PublicKeyHash? pubKeyHash;

/// If the [AtValue] is public data (i.e. it is not encrypted) and contains one or more new line (\n) characters,
/// then the data will be encoded, and the encoding will be set to type of encoding (e.g. "base64")
String? encoding;
Expand Down Expand Up @@ -553,7 +559,7 @@ class Metadata {
', refreshAt : ${refreshAt?.toUtc().toString()}, createdAt : ${createdAt?.toUtc().toString()}'
', updatedAt : ${updatedAt?.toUtc().toString()}, isBinary : $isBinary, isEncrypted : $isEncrypted'
', isCached : $isCached, dataSignature: $dataSignature, sharedKeyStatus: $sharedKeyStatus'
', encryptedSharedKey: $sharedKeyEnc, pubKeyCheckSum: $pubKeyCS, encoding: $encoding'
', encryptedSharedKey: $sharedKeyEnc, pubKeyHash: $pubKeyHash, encoding: $encoding'
', encKeyName: $encKeyName, encAlgo: $encAlgo, ivNonce: $ivNonce'
', skeEncKeyName: $skeEncKeyName, skeEncAlgo: $skeEncAlgo}';
}
Expand Down Expand Up @@ -595,6 +601,14 @@ class Metadata {
if (pubKeyCS.isNotNullOrEmpty) {
sb.write(':${AtConstants.sharedWithPublicKeyCheckSum}:$pubKeyCS');
}
if (pubKeyHash != null && pubKeyHash!.hash.isNotNullOrEmpty) {
sb.write(
':${AtConstants.sharedWithPublicKeyHashValue}:${pubKeyHash!.hash}');
}
if (pubKeyHash != null && pubKeyHash!.publicKeyHashingAlgo != null) {
sb.write(
':${AtConstants.sharedWithPublicKeyHashAlgo}:${pubKeyHash!.publicKeyHashingAlgo}');
}
if (encoding.isNotNullOrEmpty) {
sb.write(':${AtConstants.encoding}:$encoding');
}
Expand Down Expand Up @@ -667,6 +681,9 @@ class Metadata {
if (fullJson || pubKeyCS != null) {
map[AtConstants.sharedWithPublicKeyCheckSum] = pubKeyCS;
}
if (fullJson || pubKeyHash != null) {
map[AtConstants.sharedWithPublicKeyHash] = pubKeyHash!.toJson();
}
if (fullJson || encoding != null) {
map[AtConstants.encoding] = encoding;
}
Expand Down Expand Up @@ -734,6 +751,7 @@ class Metadata {
metaData.sharedKeyStatus = json[AtConstants.sharedKeyStatus];
metaData.sharedKeyEnc = json[AtConstants.sharedKeyEncrypted];
metaData.pubKeyCS = json[AtConstants.sharedWithPublicKeyCheckSum];
metaData.pubKeyHash = json[AtConstants.sharedWithPublicKeyHash];
metaData.encoding = json[AtConstants.encoding];
metaData.encKeyName = json[AtConstants.encryptingKeyName];
metaData.encAlgo = json[AtConstants.encryptingAlgo];
Expand Down Expand Up @@ -769,6 +787,7 @@ class Metadata {
isCached == other.isCached &&
sharedKeyEnc == other.sharedKeyEnc &&
pubKeyCS == other.pubKeyCS &&
pubKeyHash == other.pubKeyHash &&
encoding == other.encoding &&
encKeyName == other.encKeyName &&
encAlgo == other.encAlgo &&
Expand Down Expand Up @@ -797,6 +816,7 @@ class Metadata {
isCached.hashCode ^
sharedKeyEnc.hashCode ^
pubKeyCS.hashCode ^
pubKeyHash.hashCode ^
encoding.hashCode ^
encKeyName.hashCode ^
encAlgo.hashCode ^
Expand Down
30 changes: 30 additions & 0 deletions packages/at_commons/lib/src/keystore/public_key_hash.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// Represents hash of an atsign's public encryption key and the hashing algorithm used
class PublicKeyHash {
String? hash;
PublicKeyHashingAlgo? publicKeyHashingAlgo;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these nullable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

field should not be nullable. I have removed nullable fields


@override
String toString() {
return 'PublicKeyHash{hash: $hash, publicKeyHashingAlgo: $publicKeyHashingAlgo}';
}

Map toJson() {
var map = {};
map['hash'] = hash;
map['algo'] = publicKeyHashingAlgo;
return map;
}

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is PublicKeyHash &&
runtimeType == other.runtimeType &&
hash == other.hash &&
publicKeyHashingAlgo == other.publicKeyHashingAlgo;

@override
int get hashCode => hash.hashCode ^ publicKeyHashingAlgo.hashCode;
}

enum PublicKeyHashingAlgo { sha256, sha512 }