From 320f4a8d4caf4547662cb94764491cd6076705f9 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 16 May 2023 00:59:50 +0200 Subject: [PATCH] fix(xep): Fix issues with enhanced enums --- packages/moxxmpp/CHANGELOG.md | 1 + packages/moxxmpp/lib/src/namespaces.dart | 6 --- packages/moxxmpp/lib/src/xeps/xep_0300.dart | 49 +++++++++++++-------- packages/moxxmpp/lib/src/xeps/xep_0449.dart | 2 +- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/packages/moxxmpp/CHANGELOG.md b/packages/moxxmpp/CHANGELOG.md index 1765b4a..6db9a04 100644 --- a/packages/moxxmpp/CHANGELOG.md +++ b/packages/moxxmpp/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.3.2 - **BREAKING**: Remove `lastResource` from `XmppConnection`'s `connect` method. Instead, set the `StreamManagementNegotiator`'s `resource` attribute instead. Since the resource can only really be restored by stream management, this is no issue. +- **BREAKING**: Changed order of parameters of `CryptographicHashManager.hashFromData` ## 0.3.1 diff --git a/packages/moxxmpp/lib/src/namespaces.dart b/packages/moxxmpp/lib/src/namespaces.dart index a483929..f0be542 100644 --- a/packages/moxxmpp/lib/src/namespaces.dart +++ b/packages/moxxmpp/lib/src/namespaces.dart @@ -74,12 +74,6 @@ const forwardedXmlns = 'urn:xmpp:forward:0'; // XEP-0300 const hashXmlns = 'urn:xmpp:hashes:2'; const hashFunctionNameBaseXmlns = 'urn:xmpp:hash-function-text-names'; -const hashSha256 = 'sha-256'; -const hashSha512 = 'sha-512'; -const hashSha3256 = 'sha3-256'; -const hashSha3512 = 'sha3-512'; -const hashBlake2b256 = 'blake2b-256'; -const hashBlake2b512 = 'blake2b-512'; // XEP-0308 const lmcXmlns = 'urn:xmpp:message-correct:0'; diff --git a/packages/moxxmpp/lib/src/xeps/xep_0300.dart b/packages/moxxmpp/lib/src/xeps/xep_0300.dart index 6fdda6a..f1d5f1c 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0300.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0300.dart @@ -4,6 +4,15 @@ import 'package:moxxmpp/src/managers/namespaces.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stringxml.dart'; +/// Hash names +const _hashSha256 = 'sha-256'; +const _hashSha512 = 'sha-512'; +const _hashSha3256 = 'sha3-256'; +const _hashSha3512 = 'sha3-512'; +const _hashBlake2b256 = 'blake2b-256'; +const _hashBlake2b512 = 'blake2b-512'; + +/// Helper method for building a element according to XEP-0300. XMLNode constructHashElement(HashFunction hash, String value) { return XMLNode.xmlns( tag: 'hash', @@ -37,17 +46,17 @@ enum HashFunction { /// - XEP-0300 factory HashFunction.fromName(String name) { switch (name) { - case hashSha256: + case _hashSha256: return HashFunction.sha256; - case hashSha512: + case _hashSha512: return HashFunction.sha512; - case hashSha3256: + case _hashSha3256: return HashFunction.sha3_256; - case hashSha3512: + case _hashSha3512: return HashFunction.sha3_512; - case hashBlake2b256: + case _hashBlake2b256: return HashFunction.blake2b256; - case hashBlake2b512: + case _hashBlake2b512: return HashFunction.blake2b512; } @@ -58,17 +67,17 @@ enum HashFunction { String toName() { switch (this) { case HashFunction.sha256: - return hashSha256; + return _hashSha256; case HashFunction.sha512: - return hashSha512; + return _hashSha512; case HashFunction.sha3_256: - return hashSha3512; + return _hashSha3512; case HashFunction.sha3_512: - return hashSha3512; + return _hashSha3512; case HashFunction.blake2b256: - return hashBlake2b256; + return _hashBlake2b256; case HashFunction.blake2b512: - return hashBlake2b512; + return _hashBlake2b512; } } } @@ -81,17 +90,19 @@ class CryptographicHashManager extends XmppManagerBase { @override List getDiscoFeatures() => [ - '$hashFunctionNameBaseXmlns:$hashSha256', - '$hashFunctionNameBaseXmlns:$hashSha512', - //'$hashFunctionNameBaseXmlns:$hashSha3256', - //'$hashFunctionNameBaseXmlns:$hashSha3512', - //'$hashFunctionNameBaseXmlns:$hashBlake2b256', - '$hashFunctionNameBaseXmlns:$hashBlake2b512', + '$hashFunctionNameBaseXmlns:$_hashSha256', + '$hashFunctionNameBaseXmlns:$_hashSha512', + //'$hashFunctionNameBaseXmlns:$_hashSha3256', + //'$hashFunctionNameBaseXmlns:$_hashSha3512', + //'$hashFunctionNameBaseXmlns:$_hashBlake2b256', + '$hashFunctionNameBaseXmlns:$_hashBlake2b512', ]; + /// Compute the raw hash value of [data] using the algorithm specified by [function]. + /// If the function is not supported, an exception will be thrown. static Future> hashFromData( - List data, HashFunction function, + List data, ) async { // TODO(PapaTutuWawa): Implement the others as well HashAlgorithm algo; diff --git a/packages/moxxmpp/lib/src/xeps/xep_0449.dart b/packages/moxxmpp/lib/src/xeps/xep_0449.dart index 426d208..07d5127 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0449.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0449.dart @@ -217,11 +217,11 @@ class StickerPack { // Calculate the hash final rawHash = await CryptographicHashManager.hashFromData( + hashFunction, [ ...metaString, ...stickersString, ], - hashFunction, ); return base64.encode(rawHash).substring(0, 24); }