一种计算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;
    }
原文地址:https://www.cnblogs.com/passedbylove/p/11842851.html