tlsutility: move hex encoding into a separate function BinaryToHex

This commit is contained in:
Julian Brost 2021-09-21 12:56:10 +02:00
parent f976e351f4
commit 6cd3a483a0
2 changed files with 14 additions and 9 deletions

View File

@ -844,15 +844,7 @@ String SHA1(const String& s, bool binary)
if (binary)
return String(reinterpret_cast<const char*>(digest), reinterpret_cast<const char *>(digest + SHA_DIGEST_LENGTH));
static const char hexdigits[] = "0123456789abcdef";
char output[SHA_DIGEST_LENGTH*2+1];
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
output[2*i] = hexdigits[digest[i] >> 4];
output[2*i + 1] = hexdigits[digest[i] & 0xf];
}
output[2*SHA_DIGEST_LENGTH] = 0;
return output;
return BinaryToHex(digest, SHA_DIGEST_LENGTH);
}
String SHA256(const String& s)
@ -930,6 +922,18 @@ String RandomString(int length)
return result;
}
String BinaryToHex(const unsigned char* data, size_t length) {
static const char hexdigits[] = "0123456789abcdef";
String output(2*length, 0);
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
output[2 * i] = hexdigits[data[i] >> 4];
output[2 * i + 1] = hexdigits[data[i] & 0xf];
}
return output;
}
bool VerifyCertificate(const std::shared_ptr<X509> &caCertificate, const std::shared_ptr<X509> &certificate, const String& crlFile)
{
X509_STORE *store = X509_STORE_new();

View File

@ -61,6 +61,7 @@ String PBKDF2_SHA256(const String& password, const String& salt, int iterations)
String SHA1(const String& s, bool binary = false);
String SHA256(const String& s);
String RandomString(int length);
String BinaryToHex(const unsigned char* data, size_t length);
bool VerifyCertificate(const std::shared_ptr<X509>& caCertificate, const std::shared_ptr<X509>& certificate, const String& crlFile);
bool IsCa(const std::shared_ptr<X509>& cacert);