github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/cry/README.md (about)

     1  # gocrypt
     2  
     3  `gocrypt` is encryption/decryption library for golang.
     4  
     5  Library gocrypt provides a library to do encryption instruct with go field. The library gocrypt provide instruct tag encryption or inline encryption and decryption
     6  
     7  ## The package supported:
     8  
     9  ### **DES3** — Triple Data Encryption Standard
    10  The DES ciphers are primarily supported for PBE standard that provides the option of generating an encryption key based on a passphrase.
    11  
    12  ### **AES** — Advanced Encryption Standard
    13  The AES cipher is the current U.S. government standard for all software and is recognized worldwide.
    14  
    15  ### **RC4** — stream chipper
    16  The RC4 is supplied for situations that call for fast encryption, but not strong encryption. RC4 is ideal for situations that require a minimum of encryption.
    17  
    18  ## The Idea
    19  
    20  ### Sample JSON Payload
    21  
    22  Before the encryption:
    23  ```json
    24  {
    25      "a": "Sample plain text",
    26      "b": {
    27          c: "Sample plain text 2"
    28      }
    29  }
    30  ```
    31  
    32  After the encryption:
    33  
    34  ```json
    35  {
    36      "profile": "akldfjiaidjfods==",
    37      "id": {
    38          c: "Ijdsifsjiek18239=="
    39      }
    40  }
    41  ```
    42  
    43  ### Struct Tag Field Implementation
    44  
    45  ```go
    46  type A struct {
    47      A string    `json:"a" gocrypt:"aes"`
    48      B B         `json:"b"`
    49  }
    50  
    51  type B struct {
    52      C string    `json:"c" gocrypt:"aes"`
    53  }
    54  ```
    55  
    56  ## Full Sample
    57  
    58  ```go
    59  package main
    60  
    61  import (
    62  	"encoding/json"
    63  	"fmt"
    64  	"log"
    65  	"time"
    66  
    67  	"github.com/firdasafridi/gocrypt"
    68  )
    69  
    70  // Data contains identity and profile user
    71  type Data struct {
    72  	Profile  *Profile  `json:"profile"`
    73  	Identity *Identity `json:"identity"`
    74  }
    75  
    76  // Profile contains name and phone number user
    77  type Profile struct {
    78  	Name        string `json:"name"`
    79  	PhoneNumber string `json:"phone_number" gocrypt:"aes"`
    80  }
    81  
    82  // Identity contains id, license number, and expired date
    83  type Identity struct {
    84  	ID            string    `json:"id" gocrypt:"aes"`
    85  	LicenseNumber string    `json:"license_number" gocrypt:"aes"`
    86  	ExpiredDate   time.Time `json:"expired_date"`
    87  }
    88  
    89  const (
    90  	// it's random string must be hexa  a-f & 0-9
    91  	aeskey = "fa89277fb1e1c344709190deeac4465c2b28396423c8534a90c86322d0ec9dcf"
    92  )
    93  
    94  func main() {
    95  
    96  	// define AES option
    97  	aesOpt, err := gocrypt.NewAESOpt(aeskey)
    98  	if err != nil {
    99  		log.Println("ERR", err)
   100  		return
   101  	}
   102  
   103  	data := &Data{
   104  		Profile: &Profile{
   105  			Name:        "Batman",
   106  			PhoneNumber: "+62123123123",
   107  		},
   108  		Identity: &Identity{
   109  			ID:            "12345678",
   110  			LicenseNumber: "JSKI-123-456",
   111  		},
   112  	}
   113  
   114  	cryptRunner := gocrypt.New(&gocrypt.Option{
   115  		AESOpt: aesOpt,
   116  	})
   117  
   118  	err = cryptRunner.Encrypt(data)
   119  	if err != nil {
   120  		log.Println("ERR", err)
   121  		return
   122  	}
   123  	strEncrypt, _ := json.Marshal(data)
   124  	fmt.Println("Encrypted:", string(strEncrypt))
   125  
   126  	err = cryptRunner.Decrypt(data)
   127  	if err != nil {
   128  		log.Println("ERR", err)
   129  		return
   130  	}
   131  	strDecrypted, _ := json.Marshal(data)
   132  	fmt.Println("Decrypted:", string(strDecrypted))
   133  }
   134  
   135  ```
   136  
   137  ### Others
   138  `gocrypt` also supported inline encryption/decryption.
   139  
   140  ```go
   141  	func main() {
   142          // sample plain text
   143          sampleText := "Halo this is encrypted text!!!"
   144  
   145          // define DES option
   146          desOpt, err := gocrypt.NewDESOpt(key)
   147          if err != nil {
   148              log.Println("ERR", err)
   149              return
   150          }
   151  
   152          // Encrypt text using DES algorithm
   153          cipherText, err := desOpt.Encrypt([]byte(sampleText))
   154          if err != nil {
   155              log.Println("ERR", err)
   156              return
   157          }
   158          fmt.Println("Encrypted data:", cipherText)
   159  
   160          // Decrypt text using DES algorithm
   161          plainText, err := desOpt.Decrypt([]byte(cipherText))
   162          if err != nil {
   163              log.Println("ERR", err)
   164              return
   165          }
   166          fmt.Println("Decrypted data:", plainText)
   167      }
   168  ```
   169  
   170  ## Limitation
   171  `gocrypt` only supports the string type. Need more research & development to support the library for more type data.