栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Adler32 算法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;

        /// Performs the hash algorithm on given data array.
        /// Input data.
        /// The position to begin reading from.
        /// How many bytes in the bytesArray to read.
        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);
        }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/827052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号