X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fsha1.c;h=205b82fe0f5f5c83d78c429d551ed10864e296dc;hb=a85c0bbcfd19fcd88ee9966a86dd83107dfd6603;hp=f73f2d6477e28e32ceed79f2e57e78544bd6190e;hpb=5eccf359391f7fe2cdb0edbaaf5680895c115218;p=sliver-openvswitch.git diff --git a/lib/sha1.c b/lib/sha1.c index f73f2d647..205b82fe0 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; +} +