From: Ben Pfaff Date: Wed, 4 Nov 2009 23:12:54 +0000 (-0800) Subject: sha1: Add functions, macros for converting digests to and from strings. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7f1bf58e44a3449ba6dcf637a51191006bd2324;p=openvswitch sha1: Add functions, macros for converting digests to and from strings. These will be used in the upcoming OVSDB. The SHA1_FMT and SHA1_ARGS macros are arguably absurd, but they are also convenient. --- 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; +} + diff --git a/lib/sha1.h b/lib/sha1.h index 75d3533d..9a372775 100644 --- a/lib/sha1.h +++ b/lib/sha1.h @@ -29,11 +29,12 @@ #ifndef SHA1_H #define SHA1_H +#include #include #include -/* Size of the SHA1 digest. */ -#define SHA1_DIGEST_SIZE 20 +#define SHA1_DIGEST_SIZE 20 /* Size of the SHA1 digest. */ +#define SHA1_HEX_DIGEST_LEN 40 /* Length of SHA1 digest as hex in ASCII. */ /* SHA1 context structure. */ struct sha1_ctx { @@ -48,4 +49,18 @@ void sha1_update(struct sha1_ctx *, const void *, size_t); void sha1_final(struct sha1_ctx *, uint8_t digest[SHA1_DIGEST_SIZE]); void sha1_bytes(const void *, size_t, uint8_t digest[SHA1_DIGEST_SIZE]); +#define SHA1_FMT \ + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \ + "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" +#define SHA1_ARGS(DIGEST) \ + ((DIGEST)[0]), ((DIGEST)[1]), ((DIGEST)[2]), ((DIGEST)[3]), \ + ((DIGEST)[4]), ((DIGEST)[5]), ((DIGEST)[6]), ((DIGEST)[7]), \ + ((DIGEST)[8]), ((DIGEST)[9]), ((DIGEST)[10]), ((DIGEST)[11]), \ + ((DIGEST)[12]), ((DIGEST)[13]), ((DIGEST)[14]), ((DIGEST)[15]), \ + ((DIGEST)[16]), ((DIGEST)[17]), ((DIGEST)[18]), ((DIGEST)[19]) + +void sha1_to_hex(const uint8_t digest[SHA1_DIGEST_SIZE], + char hex[SHA1_HEX_DIGEST_LEN + 1]); +bool sha1_from_hex(uint8_t digest[SHA1_DIGEST_SIZE], const char *hex); + #endif /* sha1.h */