Selaa lähdekoodia

LibCrypto: Add a Formatter for Crypto::Hash::Digest<>

Nico Weber 2 vuotta sitten
vanhempi
commit
c10a02c405
1 muutettua tiedostoa jossa 14 lisäystä ja 0 poistoa
  1. 14 0
      Userland/Libraries/LibCrypto/Hash/HashFunction.h

+ 14 - 0
Userland/Libraries/LibCrypto/Hash/HashFunction.h

@@ -7,6 +7,7 @@
 #pragma once
 
 #include <AK/ByteBuffer.h>
+#include <AK/Format.h>
 #include <AK/StringView.h>
 #include <AK/Types.h>
 
@@ -58,3 +59,16 @@ protected:
     virtual ~HashFunction() = default;
 };
 }
+
+template<size_t DigestS>
+struct AK::Formatter<Crypto::Hash::Digest<DigestS>> : StandardFormatter {
+    ErrorOr<void> format(FormatBuilder& builder, Crypto::Hash::Digest<DigestS> const& digest)
+    {
+        for (size_t i = 0; i < digest.Size; ++i) {
+            if (i > 0 && i % 4 == 0)
+                TRY(builder.put_padding('-', 1));
+            TRY(builder.put_u64(digest.data[i], 16, false, false, true, FormatBuilder::Align::Right, 2));
+        }
+        return {};
+    }
+};