Git Product home page Git Product logo

trust's Introduction

trust

介绍:

一个简单的高性能的认证模块, 防止接口完全公开被无脑调用

适用于内网, 低延时,可信度较高环境中的同一套服务的内部使用
仅需要使用统一的 KEY, 做简单的类似防盗链的加密认证方式

Decode 系列函数返回两个结果, 是否验证通过和错误码,
当通过当时候, 错误码为 nil, 不通过原因可通过错误码获得
其中 NoErr 系列只返回是否验证通过

Encode 系列函数需要注意, 当使用返回两个结果的函数时候,
其返回的密钥串中不包含时间戳信息

特点:

  1. 性能极高, 获取认证串仅耗时 95ns, 验证是否合法耗时约 300-400ns
  2. 线程安全

Example

func Example() {
	tru := New("hello world, hello trust", 2) // 单位为 s
	hashOne := tru.EncodeOne()

	time.Sleep(time.Duration(1) * time.Second)
	s1, e1 := tru.DecodeOne(hashOne)

	time.Sleep(time.Duration(2) * time.Second)
	s2, e2 := tru.DecodeOne(hashOne)

	if s1 && e1 == nil && !s2 && e2 != nil {
		fmt.Println("Good")
	} else {
		fmt.Println("Bad")
	}
	// output: Good
}

Benchmark

BenchmarkDecodeOne-4              	 2788777	       424.7 ns/op
BenchmarkDecodeOneNoErr-4         	 2813392	       425.2 ns/op
BenchmarkDecodeAtIntT-4           	 3227451	       372.7 ns/op
BenchmarkDecodeAtIntTNoErr-4      	 3134587	       377.9 ns/op
BenchmarkDecodeAtStringT-4        	 3515676	       341.1 ns/op
BenchmarkDecodeAtStringTNoErr-4   	 3550251	       337.5 ns/op
BenchmarkEncodeOne-4              	11798652	        94.49 ns/op
BenchmarkEncodeAtIntT-4           	12508225	        95.14 ns/op
BenchmarkEncodeStringT-4          	12508938	        94.23 ns/op

trust's People

Stargazers

 avatar  avatar

Watchers

 avatar

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.