xref: /aosp_15_r20/external/pdfium/testing/utils/hash.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2019 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/utils/hash.h"
6 
7 #include "core/fdrm/fx_crypt.h"
8 
CryptToBase16(const uint8_t * digest)9 std::string CryptToBase16(const uint8_t* digest) {
10   static char const zEncode[] = "0123456789abcdef";
11   std::string ret;
12   ret.resize(32);
13   for (int i = 0, j = 0; i < 16; i++, j += 2) {
14     uint8_t a = digest[i];
15     ret[j] = zEncode[(a >> 4) & 0xf];
16     ret[j + 1] = zEncode[a & 0xf];
17   }
18   return ret;
19 }
20 
GenerateMD5Base16(pdfium::span<const uint8_t> data)21 std::string GenerateMD5Base16(pdfium::span<const uint8_t> data) {
22   uint8_t digest[16];
23   CRYPT_MD5Generate(data, digest);
24   return CryptToBase16(digest);
25 }
26