日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Adler-32校验算法

發布時間:2024/4/11 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Adler-32校验算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Adler-32校驗算法

Adler-32是Mark Adler發明的校驗和算法,和32位CRC校驗算法一樣,都是保護數據防止意外更改的算法,但是這個算法較容易被偽造,所以是不安全的保護措施。但是比CRC好點的是,它計算的很快。這個算法那是從Fletcher校驗和算法中修改過來的,原始的算法形式略快,但是可依賴性并不高。

Adler-32的一種滾動哈希版本被用在了rsync工具中

Adler-32通過求解兩個16位的數值A、B實現,并將結果連結成一個32位整數.

A就是字符串中每個字節的和,而B是A在相加時每一步的階段值之和。在Adler-32開始運行時,A初始化為1,B初始化為0,最后的校驗和要模上65521(繼216之后的最小素數)。

A = 1 + D1 + D2 + ... + Dn (mod 65521) B = (1 + D1) + (1 + D1 + D2) + ... + (1 + D1 + D2 + ... + Dn) (mod 65521)= n×D1 + (n-1)×D2 + (n-2)×D3 + ... + Dn + n (mod 65521) Adler-32(D) = B × 65536 + A 其中D為字符串的字節,n是D的字節長度

下面舉例使用Adler-32校驗算法產生字符串"Wikipedia"的校驗和:

ASCII code A BW: 87 1 + 87 = 88 0 + 88 = 88i: 105 88 + 105 = 193 88 + 193 = 281k: 107 193 + 107 = 300 281 + 300 = 581i: 105 300 + 105 = 405 581 + 405 = 986p: 112 405 + 112 = 517 986 + 517 = 1503e: 101 517 + 101 = 618 1503 + 618 = 2121d: 100 618 + 100 = 718 2121 + 718 = 2839i: 105 718 + 105 = 823 2839 + 823 = 3662a: 97 823 + 97 = 920 3662 + 920 = 4582A = 920 = 398 hexB = 4582 = 11E6 hexOutput: 11E60398 hex 都默默的進行過了模65521操作了

例子(一個不高效,但是很直接的實現):

const int MOD_ADLER = 65521;uint32_t adler32(unsigned char *data, int32_t len) /* where data is the location of the data in physical memory andlen is the length of the data in bytes */ {uint32_t a = 1, b = 0;int32_t index;/* Process each byte of the data in order */for (index = 0; index < len; ++index){a = (a + data[index]) % MOD_ADLER;b = (b + a) % MOD_ADLER;}return (b << 16) | a; }

Advantages and disadvantages[edit]Like the standard CRC-32, the Adler-32 checksum can be forged easily and is therefore unsafe for protecting against intentional modification.It's faster than CRC-32 on many platforms.Adler-32 has a weakness for short messages with few hundred bytes, because the checksums for these messages have a poor coverage of the 32 available bits.

Date: 2014-10-27T20:58+0800

Author: kirchhoff

Org?version 7.9.3f with?Emacs?version 24

Validate XHTML 1.0

c++實現:

#pragma once#ifndef _ADLER32_H_ #define _ADLER32_H_unsigned int Adler32(const unsigned char *buf, unsigned int len);#endif //!_ADLER32_H_
#include "stdafx.h" #include "Adler32.h"/* adler32.c -- compute the Adler-32 checksum of a data stream * Copyright (C) 1995-2007 Mark Adler * For conditions of distribution and use, see copyright notice in zlib.h *//* @(#) $Id$ */static const unsigned long BASE = 65521; /* largest prime smaller than 65536 */ static const unsigned int NMAX = 5552; /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); #define DO16(buf) DO8(buf,0); DO8(buf,8);/* use NO_DIVIDE if your processor does not do division in hardware */ #ifdef NO_DIVIDE # define MOD(a) \do { \if (a >= (BASE << 16)) a -= (BASE << 16); \if (a >= (BASE << 15)) a -= (BASE << 15); \if (a >= (BASE << 14)) a -= (BASE << 14); \if (a >= (BASE << 13)) a -= (BASE << 13); \if (a >= (BASE << 12)) a -= (BASE << 12); \if (a >= (BASE << 11)) a -= (BASE << 11); \if (a >= (BASE << 10)) a -= (BASE << 10); \if (a >= (BASE << 9)) a -= (BASE << 9); \if (a >= (BASE << 8)) a -= (BASE << 8); \if (a >= (BASE << 7)) a -= (BASE << 7); \if (a >= (BASE << 6)) a -= (BASE << 6); \if (a >= (BASE << 5)) a -= (BASE << 5); \if (a >= (BASE << 4)) a -= (BASE << 4); \if (a >= (BASE << 3)) a -= (BASE << 3); \if (a >= (BASE << 2)) a -= (BASE << 2); \if (a >= (BASE << 1)) a -= (BASE << 1); \if (a >= BASE) a -= BASE; \} while (0) # define MOD4(a) \do { \if (a >= (BASE << 4)) a -= (BASE << 4); \if (a >= (BASE << 3)) a -= (BASE << 3); \if (a >= (BASE << 2)) a -= (BASE << 2); \if (a >= (BASE << 1)) a -= (BASE << 1); \if (a >= BASE) a -= BASE; \} while (0) #else # define MOD(a) (a %= BASE) # define MOD4(a) (a %= BASE) #endif/* ========================================================================= */ unsigned int Adler32(const unsigned char *buf, unsigned int len) {unsigned long adler = 1;unsigned long sum2 = 0;unsigned int n;/* initial Adler-32 value (deferred check for len == 1 speed) */if (NULL == buf)return adler;/* in case user likes doing a byte at a time, keep it fast */if (len == 1) {adler += buf[0];if (adler >= BASE)adler -= BASE;sum2 += adler;if (sum2 >= BASE)sum2 -= BASE;return adler | (sum2 << 16);}/* in case short lengths are provided, keep it somewhat fast */if (len < 16) {while (len--) {adler += *buf++;sum2 += adler;}if (adler >= BASE)adler -= BASE;MOD4(sum2); /* only added so many BASE's */return adler | (sum2 << 16);}/* do length NMAX blocks -- requires just one modulo operation */while (len >= NMAX) {len -= NMAX;n = NMAX / 16; /* NMAX is divisible by 16 */do {DO16(buf); /* 16 sums unrolled */buf += 16;} while (--n);MOD(adler);MOD(sum2);}/* do remaining bytes (less than NMAX, still just one modulo) */if (len) { /* avoid modulos if none remaining */while (len >= 16) {len -= 16;DO16(buf);buf += 16;}while (len--) {adler += *buf++;sum2 += adler;}MOD(adler);MOD(sum2);}/* return recombined sums */return (unsigned int)(adler | (sum2 << 16)); }

總結

以上是生活随笔為你收集整理的Adler-32校验算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。