diff options
author | Ben Pfaff <blp@nicira.com> | 2009-11-04 15:12:54 -0800 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2009-11-04 15:24:40 -0800 |
commit | e7f1bf58e44a3449ba6dcf637a51191006bd2324 (patch) | |
tree | f78774467343fa23961fece9082336ae27e4c47d /lib/sha1.c | |
parent | 0611aea043269d0e3dad7090b590b196aa378382 (diff) |
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.
Diffstat (limited to 'lib/sha1.c')
-rw-r--r-- | lib/sha1.c | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -31,7 +31,9 @@ #include <config.h> #include "sha1.h" +#include <ctype.h> #include <string.h> +#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; +} + |