Go Engineer System Course 005 [Learning Notes]

For microservice development, create a microservice project where all project microservices will reside. Create `joyshop_srv`. We need to create user login and registration services, so we will create another directory `user_srv` under the project directory, along with `user_srv/global` (for global object creation and initialization), `user_srv/handler` (for business logic code), `user_srv/model` (for user-related models), `user_srv/pro...`

Microservice Development

Create a microservice project. All project microservices will reside within this project. Create joyshop_srv. Since we are not creating a user login and registration service, we will create another directory under the project directory:
user_srv and user_srv/global (global object creation and initialization)
user_srv/handler (business logic code)
user_srv/model (user-related models)
user_srv/proto (user-related models)
main.go service startup file

We use MD5 encryption for passwords.

1. Message Digest Algorithm 5 (MD5) Information Digest Algorithm

MD5 is a common hashing algorithm with the following main characteristics:

  1. Compressibility
    For data of any length, the calculated MD5 value will always have a fixed length.
  2. Ease of Computation
    It is very easy to compute the MD5 value from the original data.
  3. Modification Resistance
    Any modification to the original data, even a single byte, will result in a significantly different MD5 value.
  4. Strong Collision Resistance
    It is extremely difficult to find two different pieces of data that produce the same MD5 value.
  5. Irreversibility
    It is irreversible; the original data cannot be recovered from an MD5 value.

MD5 Salt Encryption

1. Purpose of Salting

To enhance the security of MD5 encryption and prevent rainbow table attacks, a "salt" value is typically added to the original data before MD5 encryption.

2. Salting Methods

  1. Combine by generating a random number and an MD5-generated string
  2. Concatenate the randomly generated salt value with the original password before performing MD5 encryption.
  3. E.g.: md5( password + salt )
  4. Store both MD5 value and salt value in the database
  5. During registration: Generate a salt, compute the salted MD5, and store both in the database.
  6. During verification: Retrieve the salt, re-encrypt, and compare the MD5 values.
// 设置加密参数
options := &password.Options{
 SaltLen:      16,
 Iterations:   100,
 KeyLen:       32,
 HashFunction: sha512.New,
}

// 1. 加密
salt, encodedPwd := password.Encode("your_password", options)
final := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodedPwd)

// 2. 拆分(模拟从数据库读取)
parts := strings.Split(final, "$")
saltFromDb := parts[2]
hashFromDb := parts[3]

// 3. 验证
ok := password.Verify("your_password", saltFromDb, hashFromDb, options)
fmt.Println("验证是否通过:", ok)

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://www.walker-learn.xyz/archives/4779

(0)
Walker的头像Walker
上一篇 Nov 25, 2025 05:00
下一篇 Nov 25, 2025 03:00

Related Posts

  • In-depth Understanding of ES6 001 [Study Notes]

    Block-Level Scope Binding
    Previously, `var` variable declarations, regardless of where they were declared, were considered to be declared at the top of their scope. Since functions are first-class citizens, the typical order was `function functionName()`, followed by `var variable`.

    Block-Level Declarations
    Block-level declarations are used to declare variables that cannot be accessed outside the scope of a specified block. Block-level scope exists in:
    - Inside functions
    - Within blocks (the region between `{` and `}`)

    Temporal Dead Zone
    When the JavaScript engine scans code and finds variable declarations, it either hoists them to the top of the scope...

    Personal Mar 8, 2025
    1.8K00
  • Go Engineer Structured Course 011 [Learning Notes]

    Inverted Index for Queries
    1. What is an Inverted Index?
    An Inverted Index is a data structure used to quickly find documents containing specific terms. It is one of the core technologies of search engines.
    1.1 Basic Concepts
    Forward Index: Document ID → Document Content (list of terms)
    Inverted Index: Term → List of Document IDs containing the term
    1.2 Why is it called "Inverted"?
    An inverted index reverses the traditional relationship of "which terms a document contains" to "in which documents a term appears...

    Personal Nov 25, 2025
    28400
  • Go Engineer System Course 013 [Study Notes]

    Order transactions, whether deducting inventory first or later, will both affect inventory and orders. Therefore, distributed transactions must be used to address business issues (e.g., unpaid orders). One approach is to deduct inventory only after successful payment (e.g., an order was placed, but there was no inventory at the time of payment). Another common method is to deduct inventory when the order is placed, but if payment isn't made, the order is returned/released upon timeout.

    Transactions and Distributed Transactions
    1. What is a transaction?
    A transaction is an important concept in database management systems. It is a collection of database operations, which either all execute successfully, or all...

    Personal Nov 25, 2025
    30200
  • In-depth Understanding of ES6 007 [Study Notes]

    Set and Map Collections. In JS, there is an `in` operator that can determine if a property exists in an object without needing to read the object's value, returning true if it exists. However, the `in` operator also checks the object's prototype chain, so using this method is only relatively safe when the object's prototype is null. Set Collection: `let set = new Set()` `set.add(5)` `set.add("5")` `console.log(s…`

    Personal Mar 8, 2025
    1.3K00
  • Node: In-depth Yet Easy to Understand (Shengsi Garden Education) 003 [Study Notes]

    WebSocket and SSE Overview WebSocket Basics Definition: WebSocket is a full-duplex connection upgraded after an HTTP handshake, allowing clients and servers to push data bidirectionally over the same TCP channel, eliminating the need for repeated polling. Handshake Process: The client initiates an HTTP request with the Upgrade: websocket header; The server responds with 101 Switching Protocols, and both parties agree...

    Personal Nov 24, 2025
    41700
EN
简体中文 繁體中文 English