Skip to content

Latest commit

 

History

History
211 lines (190 loc) · 8.77 KB

README_VI.md

File metadata and controls

211 lines (190 loc) · 8.77 KB

open-dev

Contents

Gin Web Framework

API Examples

  • source code in demo api simples:

Redis

Rate Limit

Golang Usecase

Resize Image

Context

Thuật Toán RSA

Thuận Toán ECDSA

Serverless Framework

Aws

Lambda

design pattern

Lợi và Hại của việc sử dụng UUIDs:

Lợi ích

  • Phù hợp cho việc tạo ra 1 id sử dụng cho distributed system, khả năng bị trùng giữa các hệ thống là vô cùng thấp.
  • UUIDs là tạo ra ngẫu nhiên, nên hầu như không đoán trước được, phù hợp cho các giao dịch có tính bảo mật cao.

Tác hại

  • Độ dài của UUIDs, với 128-bit (32 char) sẽ tốn nơi lưu trữ.
  • UUIDs là không tạo ra theo tuần tự thời gian như time-stamped.

https hoạt động như thế nào

  • Dữ liệu Được mã hoá và giãi mã như thế nào
    • Step1: client(browser) và server sẽ thiết lập kết nối TCP
    • Step2: client sẽ gửi "client hello" đến server. Message được gửi đi sẽ chứa danh sách các thuật toán và version của TLS có thể support. Server phản hồi "server hello", lúc nào client sẽ biết server có hỗ trợ thuật toán cũng như version TLS. Tiếp đến server sẽ gửi thêm SSL certification. Trong Certification chứa public-key, host-name, expire-date, etc. Lúc nào client sẽ kiểm tra certification có hợp lệ không.
    • Step3: Certification hợp lệ, sẽ tạo ra 1 session key, và mã hoá nó dựa vào public-key. Server sẽ nhận session-key và giải mã nó bằng private-key.
    • Step4: Lúc nào cả Client và Server điều có session-key, data sẽ được mã hoá trong quá trình giao tiếp giữa 2 bên.

sso là gì

  • Khái Niệm:

    • Hiểu một cách đơn giản thì SSO (Single Sign-On) là cơ chế xác thực, nó cho phép user đăng nhập trên nhiều hệ thống khác nhau với một ID.
  • Nó Hoạt động như thế nào:

    • step1: khi user vào hệ thống Gmail, gmail sẽ kiểm tra xem có login trước đó hay không, nếu không sẽ chuyển đến trang SSO-Authen, để user nhập thông tin login.
    • step2-3: Server SSO authentication sẽ kiểm tra thông tin, nếu hợp lệ sẽ tạo một global session và tạo token.
    • step4-7: Hệ thống Gmail sẽ kiểm tra token từ SSO trả về, và gửi lại cho user.
    • step8: Từ Gmail, user chuyển một trang khác của hệ thống google, ví dụ như youtube.
    • step9-10: Youtube sẽ kiểm tra user chưa login, sẽ chuyển token đến server sso để xác thực có hợp lệ hay không, trả về token.
    • step11-14: Hệ thống youtube sẽ kiểm tra token từ sso, và token sẽ được đăng kí trong hệ thống youtube, cuối cùng là gửi lại token cho user đã được bảo vệ.

Lưu passowrd trong database:

  • Không Nên:
    • Password lưu plain-text là không tốt vì với những người nắm hệ thống sẽ có thể nhìn thấy
    • Lưu password hash là chưa đủ, vì có thể bị tấn công, ví dụ: rainbow-tables.
    • Để giảm thiệu các rủi ro, cần thêm salt đến password.
  • Vậy Salt là gì?
    • Theo như hướng dẫn của OWASP "salt is a unique, randomly genereted string that is added to each password as part of the hashing process"
  • Lưu Password và Salt.
    • Salt ở đây không phải là secret nên có thể lưu plaintext trong database. Salt được sử dụng để đảm bảo rằng password hash là duy nhất trong hệ thống.
    • Password mình sẽ lưu kiểu: hash(pass+salt)
  • Validate Password:
    • User nhập password
    • Hệ thống sẽ dựa vào user để fetch salt được lưu dưới database.
    • Hệ thống sẽ hash(pass+salt) (1), pass là user nhập
    • So sách mã hash(1) có khớp với hash được lưu dưới database không, nếu giống nhau là password hợp lệ.

Performances

Standard

http

reuse-http

package main

import (
	"context"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptrace"
)

func main() {
	Reuse()
	//
	NonReuse()
}

// NonReuse, not reuse http
func NonReuse() {
	// client trace to log whether the request's underlying tcp connection was re-used
	clientTrace := &httptrace.ClientTrace{
		GotConn: func(info httptrace.GotConnInfo) {
			log.Printf("conn was reused: %t", info.Reused)
		},
	}
	traceCtx := httptrace.WithClientTrace(context.Background(), clientTrace)

	// 1st request
	req, err := http.NewRequestWithContext(traceCtx, http.MethodGet, "http://example.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	// 2nd request
	req, err = http.NewRequestWithContext(traceCtx, http.MethodGet, "http://example.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
}

// Reuse, reuse http client
func Reuse() {
	var (
		err error
	)
	// client trace to log whether the request's underlying tcp connection was re-used
	clientTrace := &httptrace.ClientTrace{
		GotConn: func(info httptrace.GotConnInfo) {
			log.Printf("conn was reused: %t", info.Reused)
		},
	}
	traceCtx := httptrace.WithClientTrace(context.Background(), clientTrace)

	// 1st request
	req, err := http.NewRequestWithContext(traceCtx, http.MethodGet, "http://example.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
		log.Fatal(err)
	}
	res.Body.Close()
	// 2nd request
	req, err = http.NewRequestWithContext(traceCtx, http.MethodGet, "http://example.com", nil)
	if err != nil {
		log.Fatal(err)
	}
	_, err = http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
}

tls

tls-base64

Contact: