Adler32 算法

项目中用到了分片上传文件,在校验文件的时候用到了Adler32算法,由于后端是java,调用端是c#,java是直接有这样的算法库,而c#这边好像没有找到现成的算法,所以在网上找到了一个算法。

Adler-32校验算法 C#实现_weixin_34127717的博客-CSDN博客为什么80%的码农都做不了架构师?>>> ...https://blog.csdn.net/weixin_34127717/article/details/91727793

 但是项目中,上片文章中的算法是一次传入文件数据计算出值,但在我的项目中却需要分多次计算。所以对此算法进行部分修改。

public class Adler32
    {
        public static uint checksum = 1;

        private static uint s1 = 1;
        private static uint s2 = 0;

        /// <summary>Performs the hash algorithm on given data array.</summary>
        /// <param name="bytesArray">Input data.</param>
        /// <param name="byteStart">The position to begin reading from.</param>
        /// <param name="bytesToRead">How many bytes in the bytesArray to read.</param>
        public static void ComputeHash(byte[] bytesArray, int byteStart, int bytesToRead)
        {
            int n;
            while (bytesToRead > 0)
            {
                n = (3800 > bytesToRead) ? bytesToRead : 3800;
                bytesToRead -= n;
                while (--n >= 0)
                {
                    s1 = s1 + (uint)(bytesArray[byteStart++] & 0xFF);
                    s2 = s2 + s1;
                }
                s1 %= 65521;
                s2 %= 65521;
            }
            checksum = ((s2 << 16) | s1);
        }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值