aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-01-17 16:05:16 +0000
committerChristian Grothoff <christian@grothoff.org>2016-01-17 16:05:16 +0000
commit71e181512d1cd61d3865f93f5b85b208b5720ba5 (patch)
treedd2dbd4e56236318208f1dad5acf02faa341fa2b
parent38e65c6c5268614b482d1234fea76cda11811044 (diff)
add crc8
-rw-r--r--src/include/gnunet_crypto_lib.h15
-rw-r--r--src/util/crypto_crc.c30
2 files changed, 45 insertions, 0 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 77b4d04524..23caade129 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -403,6 +403,19 @@ GNUNET_CRYPTO_seed_weak_random (int32_t seed);
/**
+ * @ingroup hash
+ * Calculate the checksum of a buffer in one step.
+ *
+ * @param buf buffer to calculate CRC over
+ * @param len number of bytes in @a buf
+ * @return crc8 value
+ */
+uint8_t
+GNUNET_CRYPTO_crc8_n (const void *buf,
+ size_t len);
+
+
+/**
* Perform an incremental step in a CRC16 (for TCP/IP) calculation.
*
* @param sum current sum, initially 0
@@ -439,6 +452,8 @@ GNUNET_CRYPTO_crc16_n (const void *buf,
size_t len);
+
+
/**
* @ingroup hash
* Compute the CRC32 checksum for the first len
diff --git a/src/util/crypto_crc.c b/src/util/crypto_crc.c
index ec2e806830..b5df01959a 100644
--- a/src/util/crypto_crc.c
+++ b/src/util/crypto_crc.c
@@ -165,5 +165,35 @@ GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
}
+/**
+ * @ingroup hash
+ * Calculate the checksum of a buffer in one step.
+ *
+ * @param buf buffer to calculate CRC over
+ * @param len number of bytes in @a buf
+ * @return crc8 value
+ */
+uint8_t
+GNUNET_CRYPTO_crc8_n (const void *buf,
+ size_t len)
+{
+ const uint8_t *data = buf;
+ unsigned int crc = 0;
+ int i;
+ int j;
+
+ for (j = len; 0 != j; j--)
+ {
+ crc ^= (*data++ << 8);
+ for (i = 8; 0 != i; i--)
+ {
+ if (0 != (crc & 0x8000))
+ crc ^= (0x1070 << 3);
+ crc <<= 1;
+ }
+ }
+ return (uint8_t) (crc >> 8);
+}
+
/* end of crypto_crc.c */