Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to encrypt int data by Go

Shulou Source: shulou.com Published: 2022-05-31 14:27:50 09月24日 Update

This article mainly explains "Go how to encrypt int data". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Go how to encrypt int type data"!

problem

Why encrypt data of type int, and what is its application scenario?

For example: there is a product details interface URL / product/1001, this situation is easy to be guessed by others, such as enter / product/1002, / product/1003 to try to check the details, then the information will be exposed, if others want to grab data, just need to grab the following ID incrementally, how to solve this problem?

For example, there is a request for a user invitation code, and the user can share his invitation code. When a new user registers with this invitation code, he or she will be rewarded to both the inviter and the invitee. Registration through URL / user/1001 means that the user's ID is 1001, so the user's ID can be easily modified. How to solve this problem?

Analysis.

In both of the above scenarios, data of type int needs to be encrypted to avoid ID disclosure.

The following characteristics need to be met:

Custom salt is supported to ensure that the encrypted one is unique. Encryption and decryption are supported. Support for multiple languages. Solution

Let's talk about the results first: I encrypted 1001 into 1oEpdk EzWA1002 encrypted into NnlzvxEORb.

The specific implementation is shown in the following code.

First: import "github.com/speps/go-hashids"

/ encryption func Encrypt (salt string, minLength int, params [] int) string {hd: = hashids.NewData () hd.Salt = salt hd.MinLength = minLength h, err: = hashids.NewWithData (hd) if err = = nil {e, err: = h.Encode (params) if err = = nil {return e}} return ""}

/ decrypt func Decrypt (salt string, minLength int, hash string) [] int {hd: = hashids.NewData () hd.Salt = salt hd.MinLength = minLength h, err: = hashids.NewWithData (hd) if err = = nil {e, err: = h.DecodeWithError (hash) if err = = nil {return e}} return [] int {}} so far I believe you have a deeper understanding of "how Go encrypts int-type data", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Encryption data type user question support content scenario details learning practical deeper unique in that case two code information interest both sides commodity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Apple MySQL Microsoft Shulou Technology