zoukankan      html  css  js  c++  java
  • 一种计算hash的思路

        /***
         * 转换请求hash,根据转换模式计算hash,防止重复发送请求,浪费服务器资源(内存、cpu、文件系统等)
         * @param mode
         * @param data
         * @return
         */
        @PerformanceMonitor
        default String computeHash(ConversionMode mode,byte[]data)
        {
    
            byte[] prefix = mode.getValue().getBytes();
    
            int length = prefix.length + data.length;
    
            ByteBuffer buffer = ByteBuffer.allocate(length);
    
            buffer.put(prefix).put(data);
    
            String hash = DigestUtils.md5DigestAsHex(buffer.array());
    
            return hash;
        }
    
        /***
         * 根据请求的类型以及数据内容生成hash
         * @param request
         * @return
         */
        default String computeHash(ConversionRequest request)
        {
            return computeHash(request.getMode(),request.getData());
    
    //        long p = bytesToLong("securityKey".getDataChunk());
    //        long hash = bytesToLong("compHash".getDataChunk());
    //
    //        for (int i = 0; i < buffer.capacity(); i++) {
    //            hash = (hash ^ buffer.get(i)) * p;
    //        }
    //
    //        hash += hash << 13;
    //        hash ^= hash >> 7;
    //        hash += hash << 3;
    //        hash ^= hash >> 17;
    //        hash += hash << 5;
    //
    //        return hash;
        }
  • 相关阅读:
    A. Generous Kefa
    1031 骨牌覆盖
    1074 约瑟夫环 V2
    1073 约瑟夫环
    1562 玻璃切割
    Ants
    1024 矩阵中不重复的元素
    1014 X^2 Mod P
    1135 原根
    1010 只包含因子2 3 5的数
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11842851.html
Copyright © 2011-2022 走看看