feat(xep): Improve API using enhanced enums
This commit is contained in:
		
							parent
							
								
									09b8613c80
								
							
						
					
					
						commit
						1fdefacd52
					
				@ -4,44 +4,38 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
 | 
			
		||||
import 'package:moxxmpp/src/namespaces.dart';
 | 
			
		||||
import 'package:moxxmpp/src/stringxml.dart';
 | 
			
		||||
 | 
			
		||||
XMLNode constructHashElement(String algo, String base64Hash) {
 | 
			
		||||
XMLNode constructHashElement(HashFunction hash, String value) {
 | 
			
		||||
  return XMLNode.xmlns(
 | 
			
		||||
    tag: 'hash',
 | 
			
		||||
    xmlns: hashXmlns,
 | 
			
		||||
    attributes: {'algo': algo},
 | 
			
		||||
    text: base64Hash,
 | 
			
		||||
    attributes: {'algo': hash.toName()},
 | 
			
		||||
    text: value,
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum HashFunction {
 | 
			
		||||
  /// SHA-256
 | 
			
		||||
  sha256,
 | 
			
		||||
 | 
			
		||||
  /// SHA-256
 | 
			
		||||
  sha512,
 | 
			
		||||
 | 
			
		||||
  /// SHA3-256
 | 
			
		||||
  sha3_256,
 | 
			
		||||
 | 
			
		||||
  /// SHA3-512
 | 
			
		||||
  sha3_512,
 | 
			
		||||
 | 
			
		||||
  /// BLAKE2b-256
 | 
			
		||||
  blake2b256,
 | 
			
		||||
  blake2b512,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extension HashNameToEnumExtension on HashFunction {
 | 
			
		||||
  String toName() {
 | 
			
		||||
    switch (this) {
 | 
			
		||||
      case HashFunction.sha256:
 | 
			
		||||
        return hashSha256;
 | 
			
		||||
      case HashFunction.sha512:
 | 
			
		||||
        return hashSha512;
 | 
			
		||||
      case HashFunction.sha3_256:
 | 
			
		||||
        return hashSha3512;
 | 
			
		||||
      case HashFunction.sha3_512:
 | 
			
		||||
        return hashSha3512;
 | 
			
		||||
      case HashFunction.blake2b256:
 | 
			
		||||
        return hashBlake2b256;
 | 
			
		||||
      case HashFunction.blake2b512:
 | 
			
		||||
        return hashBlake2b512;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
  /// BLAKE2b-512
 | 
			
		||||
  blake2b512;
 | 
			
		||||
 | 
			
		||||
HashFunction hashFunctionFromName(String name) {
 | 
			
		||||
  /// Get a HashFunction from its name [name] according to either
 | 
			
		||||
  /// - IANA's hash name register (http://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xhtml)
 | 
			
		||||
  /// - XEP-0300
 | 
			
		||||
  factory HashFunction.fromName(String name) {
 | 
			
		||||
    switch (name) {
 | 
			
		||||
      case hashSha256:
 | 
			
		||||
        return HashFunction.sha256;
 | 
			
		||||
@ -58,6 +52,25 @@ HashFunction hashFunctionFromName(String name) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    throw Exception();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Return the hash function's name according to IANA's hash name register or XEP-0300.
 | 
			
		||||
  String toName() {
 | 
			
		||||
    switch (this) {
 | 
			
		||||
      case HashFunction.sha256:
 | 
			
		||||
        return hashSha256;
 | 
			
		||||
      case HashFunction.sha512:
 | 
			
		||||
        return hashSha512;
 | 
			
		||||
      case HashFunction.sha3_256:
 | 
			
		||||
        return hashSha3512;
 | 
			
		||||
      case HashFunction.sha3_512:
 | 
			
		||||
        return hashSha3512;
 | 
			
		||||
      case HashFunction.blake2b256:
 | 
			
		||||
        return hashBlake2b256;
 | 
			
		||||
      case HashFunction.blake2b512:
 | 
			
		||||
        return hashBlake2b512;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class CryptographicHashManager extends XmppManagerBase {
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@ class FileMetadataData {
 | 
			
		||||
    this.name,
 | 
			
		||||
    this.size,
 | 
			
		||||
    required this.thumbnails,
 | 
			
		||||
    Map<String, String>? hashes,
 | 
			
		||||
    Map<HashFunction, String>? hashes,
 | 
			
		||||
  }) : hashes = hashes ?? const {};
 | 
			
		||||
 | 
			
		||||
  /// Parse [node] as a FileMetadataData element.
 | 
			
		||||
@ -31,9 +31,11 @@ class FileMetadataData {
 | 
			
		||||
    final size =
 | 
			
		||||
        sizeElement != null ? int.parse(sizeElement.innerText()) : null;
 | 
			
		||||
 | 
			
		||||
    final hashes = <String, String>{};
 | 
			
		||||
    final hashes = <HashFunction, String>{};
 | 
			
		||||
    for (final e in node.findTags('hash')) {
 | 
			
		||||
      hashes[e.attributes['algo']! as String] = e.innerText();
 | 
			
		||||
      final hashFunction =
 | 
			
		||||
          HashFunction.fromName(e.attributes['algo']! as String);
 | 
			
		||||
      hashes[hashFunction] = e.innerText();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Thumbnails
 | 
			
		||||
@ -75,7 +77,7 @@ class FileMetadataData {
 | 
			
		||||
  final int? height;
 | 
			
		||||
  final List<Thumbnail> thumbnails;
 | 
			
		||||
  final String? desc;
 | 
			
		||||
  final Map<String, String> hashes;
 | 
			
		||||
  final Map<HashFunction, String> hashes;
 | 
			
		||||
  final int? length;
 | 
			
		||||
  final String? name;
 | 
			
		||||
  final int? size;
 | 
			
		||||
 | 
			
		||||
@ -8,23 +8,9 @@ import 'package:moxxmpp/src/xeps/xep_0447.dart';
 | 
			
		||||
enum SFSEncryptionType {
 | 
			
		||||
  aes128GcmNoPadding,
 | 
			
		||||
  aes256GcmNoPadding,
 | 
			
		||||
  aes256CbcPkcs7,
 | 
			
		||||
}
 | 
			
		||||
  aes256CbcPkcs7;
 | 
			
		||||
 | 
			
		||||
extension SFSEncryptionTypeNamespaceExtension on SFSEncryptionType {
 | 
			
		||||
  String toNamespace() {
 | 
			
		||||
    switch (this) {
 | 
			
		||||
      case SFSEncryptionType.aes128GcmNoPadding:
 | 
			
		||||
        return sfsEncryptionAes128GcmNoPaddingXmlns;
 | 
			
		||||
      case SFSEncryptionType.aes256GcmNoPadding:
 | 
			
		||||
        return sfsEncryptionAes256GcmNoPaddingXmlns;
 | 
			
		||||
      case SFSEncryptionType.aes256CbcPkcs7:
 | 
			
		||||
        return sfsEncryptionAes256CbcPkcs7Xmlns;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SFSEncryptionType encryptionTypeFromNamespace(String xmlns) {
 | 
			
		||||
  factory SFSEncryptionType.fromNamespace(String xmlns) {
 | 
			
		||||
    switch (xmlns) {
 | 
			
		||||
      case sfsEncryptionAes128GcmNoPaddingXmlns:
 | 
			
		||||
        return SFSEncryptionType.aes128GcmNoPadding;
 | 
			
		||||
@ -35,6 +21,18 @@ SFSEncryptionType encryptionTypeFromNamespace(String xmlns) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    throw Exception();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  String toNamespace() {
 | 
			
		||||
    switch (this) {
 | 
			
		||||
      case SFSEncryptionType.aes128GcmNoPadding:
 | 
			
		||||
        return sfsEncryptionAes128GcmNoPaddingXmlns;
 | 
			
		||||
      case SFSEncryptionType.aes256GcmNoPadding:
 | 
			
		||||
        return sfsEncryptionAes256GcmNoPaddingXmlns;
 | 
			
		||||
      case SFSEncryptionType.aes256CbcPkcs7:
 | 
			
		||||
        return sfsEncryptionAes256CbcPkcs7Xmlns;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
 | 
			
		||||
@ -63,13 +61,15 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
 | 
			
		||||
    )!;
 | 
			
		||||
 | 
			
		||||
    // Find hashes
 | 
			
		||||
    final hashes = <String, String>{};
 | 
			
		||||
    final hashes = <HashFunction, String>{};
 | 
			
		||||
    for (final hash in element.findTags('hash', xmlns: hashXmlns)) {
 | 
			
		||||
      hashes[hash.attributes['algo']! as String] = hash.text!;
 | 
			
		||||
      final hashFunction =
 | 
			
		||||
          HashFunction.fromName(hash.attributes['algo']! as String);
 | 
			
		||||
      hashes[hashFunction] = hash.text!;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return StatelessFileSharingEncryptedSource(
 | 
			
		||||
      encryptionTypeFromNamespace(element.attributes['cipher']! as String),
 | 
			
		||||
      SFSEncryptionType.fromNamespace(element.attributes['cipher']! as String),
 | 
			
		||||
      key,
 | 
			
		||||
      iv,
 | 
			
		||||
      hashes,
 | 
			
		||||
@ -80,7 +80,7 @@ class StatelessFileSharingEncryptedSource extends StatelessFileSharingSource {
 | 
			
		||||
  final List<int> key;
 | 
			
		||||
  final List<int> iv;
 | 
			
		||||
  final SFSEncryptionType encryption;
 | 
			
		||||
  final Map<String, String> hashes;
 | 
			
		||||
  final Map<HashFunction, String> hashes;
 | 
			
		||||
  final StatelessFileSharingUrlSource source;
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
 | 
			
		||||
@ -87,7 +87,7 @@ class StickerPack {
 | 
			
		||||
    var hashValue = '';
 | 
			
		||||
    if (hashAvailable) {
 | 
			
		||||
      final hash = node.firstTag('hash', xmlns: hashXmlns)!;
 | 
			
		||||
      hashAlgorithm = hashFunctionFromName(hash.attributes['algo']! as String);
 | 
			
		||||
      hashAlgorithm = HashFunction.fromName(hash.attributes['algo']! as String);
 | 
			
		||||
      hashValue = hash.innerText();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -144,7 +144,7 @@ class StickerPack {
 | 
			
		||||
          text: summary,
 | 
			
		||||
        ),
 | 
			
		||||
        constructHashElement(
 | 
			
		||||
          hashAlgorithm.toName(),
 | 
			
		||||
          hashAlgorithm,
 | 
			
		||||
          hashValue,
 | 
			
		||||
        ),
 | 
			
		||||
 | 
			
		||||
@ -193,7 +193,7 @@ class StickerPack {
 | 
			
		||||
      final hashes = List<List<int>>.empty(growable: true);
 | 
			
		||||
      for (final hash in sticker.metadata.hashes.keys) {
 | 
			
		||||
        hashes.add([
 | 
			
		||||
          ...utf8.encode(hash),
 | 
			
		||||
          ...utf8.encode(hash.toName()),
 | 
			
		||||
          0x1f,
 | 
			
		||||
          ...utf8.encode(sticker.metadata.hashes[hash]!),
 | 
			
		||||
          0x1f,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user