Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encrypt int data by Go

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report