X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fsha1.c;h=205b82fe0f5f5c83d78c429d551ed10864e296dc;hb=a6bc4a03a44ee8a4ab346f0c1a6e21d20a1d29bd;hp=f73f2d6477e28e32ceed79f2e57e78544bd6190e;hpb=34e63086edddcae06d7c1a4fa84fec0861e50758;p=openvswitch diff --git a/lib/sha1.c b/lib/sha1.c index f73f2d64..205b82fe 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -31,7 +31,9 @@ #include #include "sha1.h" +#include #include +#include "util.h" /* a bit faster & bigger, if defined */ #define UNROLL_LOOPS @@ -279,3 +281,32 @@ sha1_bytes(const void *data, size_t n, uint8_t digest[SHA1_DIGEST_SIZE]) sha1_update(&ctx, data, n); sha1_final(&ctx, digest); } + +void +sha1_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], + char hex[SHA1_HEX_DIGEST_LEN + 1]) +{ + int i; + + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + *hex++ = "0123456789abcdef"[digest[i] >> 4]; + *hex++ = "0123456789abcdef"[digest[i] & 15]; + } + *hex = '\0'; +} + +bool +sha1_from_hex(uint8_t digest[SHA1_DIGEST_SIZE], const char *hex) +{ + int i; + + for (i = 0; i < SHA1_DIGEST_SIZE; i++) { + if (!isxdigit(hex[0]) || !isxdigit(hex[1])) { + return false; + } + digest[i] = (hexit_value(hex[0]) << 4) | hexit_value(hex[1]); + hex += 2; + } + return true; +} +