GithubHelp home page GithubHelp logo

b19g3r / checksum Goto Github PK

View Code? Open in Web Editor NEW

This project forked from qsqian/checksum

0.0 1.0 0.0 83 KB

High performance checksum framework based on java annotation and asm.(基于注解的 java 加签验签高性能框架)

License: Apache License 2.0

Batchfile 4.13% Shell 10.62% Java 85.25%

checksum's Introduction

checksum

基于 java 注解生成加签验签 checksum。

Maven Central Open Source Love

创作缘由

原来的代码中,checksum 的生成是用的工具类方法。

后来发现如下的问题:

  1. 有些字段太大,不想参与验签,但是无法方便的调整。

  2. 不同系统的 checksum 字段不同,只好把工具方法 copy 过去,改来改去。

感觉这样有很大的弊端,完全失去了灵活性。

特性

  • 基于注解的 checksum 加签验签,灵活方便

  • fluent 流式语法

  • 支持用户策略自定义

  • 高性能,和手动设置相差无几

  • 轻量级框架,jar 包只有 19kb

更新记录

更新记录

快速开始

环境要求

jdk7+

maven 3.x+

引入

<plugin>
    <groupId>com.github.houbb</groupId>
    <artifactId>checksum</artifactId>
    <version>0.0.6</version>
</plugin>

定义待加签的示例对象

  • User.java
public class User {

    @CheckField
    private String name;

    private String password;

    @CheckField(required = false)
    private String address;

    @CheckValue
    private String checksum;

    //Getter & Setter
    //toString()
}

核心注解

@CheckField 表示参与加签的字段信息,默认都是参与加签的。指定 required=false 跳过加签。

@CheckValue 表示加签结果存放的字段,该字段类型需要为 String 类型。

后期将会添加一个 String 与不同类型的转换实现,拓展应用场景。

获取签名

所有的工具类方法见 ChecksumHelper,且下面的几个方法都支持指定秘钥。

User user = User.buildUser();

final String checksum = ChecksumHelper.checkValue(user);

该方法会把 User 对象中指定 @CheckField 的字段全部进行处理,

通过指定排序后进行拼接,然后结合指定加密策略构建最后的验签结果。

填充签名

User user = User.buildUser();

ChecksumHelper.fill(user);

可以把对应的 checkValue 值默认填充到 @CheckValue 指定的字段上。

验证签名

User user = User.buildUser();

boolean isValid = ChecksumHelper.isValid(user);

会对当前的 user 对象进行加签运算,并且将加签的结果和 user 本身的签名进行对比。

引导类

ChecksumBs 引导类

为了满足更加灵活的场景,我们引入了基于 fluent-api 的 ChecksumBs 引导类。

上面的配置默认等价于:

final String checksum = ChecksumBs
        .newInstance()
        .target(user)
        .charset("UTF-8")
        .checkSum(new DefaultChecksum())
        .sort(Sorts.quick())
        .hash(Hashes.md5())
        .times(1)
        .salt(null)
        .checkFieldListCache(new CheckFieldListCache())
        .checkValueCache(new CheckValueCache())
        .checkValue();

配置说明

上面所有的配置都是可以灵活替换的,所有的实现都支持用户自定义。

属性 说明
target 待加签对象
charset 编码
checkSum 具体加签实现
sort 字段排序策略
hash 字符串加密 HASH 策略
salt 加密对应的盐值
times 加密的次数
checkFieldListCache 待加签字段的缓存实现
checkValueCache 签名字段的缓存实现

性能

背景

每次我们说到反射第一反应是方便,第二反应就是性能。

有时候往往因为关心性能,而选择手动一次次的复制,黏贴。

性能

详情见 BenchmarkTest.java

benchmark

本次进行 100w 次测试验证,耗时如下。

手动处理耗时:2505ms

注解处理耗时:2927ms

Road-MAP

  • 新增拦截器

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.