An open API service indexing awesome lists of open source software.

https://github.com/owlsome-official/cipherpayload

CipherPayload middleware for Fiber that use AES Algorithm for encrypt and decrypt payload in request and response body.
https://github.com/owlsome-official/cipherpayload

cipher encryption gofiber golang golang-library middleware payload-encryption security

Last synced: 4 months ago
JSON representation

CipherPayload middleware for Fiber that use AES Algorithm for encrypt and decrypt payload in request and response body.

Awesome Lists containing this project

README

          

# CipherPayload

[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](http://golang.org) [![Go Reference](https://pkg.go.dev/badge/github.com/owlsome-official/cipherPayload@v1.0.7.svg)](https://pkg.go.dev/github.com/owlsome-official/cipherPayload@v1.0.7) [![GitHub issues](https://img.shields.io/github/issues/owlsome-official/cipherPayload)](https://github.com/owlsome-official/cipherPayload/issues) [![GitHub forks](https://img.shields.io/github/forks/owlsome-official/cipherPayload)](https://github.com/owlsome-official/cipherPayload/network) [![GitHub stars](https://img.shields.io/github/stars/owlsome-official/cipherPayload)](https://github.com/owlsome-official/cipherPayload/stargazers)

CipherPayload middleware for Fiber that use AES Algorithm for encrypt and decrypt payload in request and response body.

## Table of Contents

- [CipherPayload](#cipherpayload)
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [Signatures](#signatures)
- [Examples](#examples)
- [Config](#config)
- [Default Config](#default-config)
- [Default Response](#default-response)
- [KeyPairs Property](#keypairs-property)
- [Payload Template](#payload-template)
- [Request](#request)
- [Response](#response)
- [Example Usage](#example-usage)

## Installation

```bash
go get -u github.com/owlsome-official/cipherPayload
```

## Signatures

```go
func New(config ...Config) fiber.Handler
```

## Examples

Import the middleware package that is part of the Fiber web framework

```go
import (
"github.com/gofiber/fiber/v2"
"github.com/owlsome-official/cipherPayload"
)
```

After you initiate your Fiber app, you can use the following possibilities:

```go
// Default middleware config
app.Use(cipherPayload.New(cipherPayload.Config{
KeyPairs: cipherPayload.KeyPairs{
AESKeyForEncrypt: []byte("AES_KEY_FOR_ENCRYPT"),
AESIVForEncrypt: []byte("AES_IV_FOR_ENCRYPT"),
AESKeyForDecrypt: []byte("AES_KEY_FOR_DECRYPT"),
AESIVForDecrypt: []byte("AES_IV_FOR_DECRYPT"),
},
}))

// Or extend your config for customization
app.Use(cipherPayload.New(cipherPayload.Config{
KeyPairs: cipherPayload.KeyPairs{
AESKeyForEncrypt: []byte("AES_KEY_FOR_ENCRYPT"),
AESIVForEncrypt: []byte("AES_IV_FOR_ENCRYPT"),
AESKeyForDecrypt: []byte("AES_KEY_FOR_DECRYPT"),
AESIVForDecrypt: []byte("AES_IV_FOR_DECRYPT"),
},
AllowMethod: []string{"POST", "OPTIONS"},
DebugMode: true,
}))
```

## Config

```go
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.

// Optional. Default: nil
Next func(c *fiber.Ctx) bool

// Required. Default: KeyPairs{}
KeyPairs KeyPairs

// Optional. Default: ["OPTIONS", "POST", "PUT", "DELETE"]
AllowMethod []string

// Optional. Default: false
DebugMode bool

// Optional. [Default: false]
StrictMode bool

// Optional. Default: true
ExcludeHealthAPI bool

// Optional. Default: BadRequestResponse
FailResponse func(c *fiber.Ctx, msg string) error

// Optional. Default: InternalServerErrorResponse
ErrorResponse func(c *fiber.Ctx, msg string) error
}
```

## Default Config

```go
var ConfigDefault = Config{
Next: nil,
KeyPairs: KeyPairs{},
AllowMethod: []string{
fiber.MethodOptions,
fiber.MethodPost,
fiber.MethodPut,
fiber.MethodDelete,
},
DebugMode: false,
StrictMode: false,
ExcludeHealthAPI: true,
FailResponse: BadRequestResponse,
ErrorResponse: InternalServerErrorResponse,
}
```

## Default Response

```go
func BadRequestResponse(c *fiber.Ctx, msg string) error { // 400
if msg == "" {
msg = "Bad Request"
}
res := fiber.Map{
"status": "bad_request",
"message": msg,
}
return c.Status(fiber.StatusBadRequest).JSON(res)
}

func InternalServerErrorResponse(c *fiber.Ctx, msg string) error { // 500
if msg == "" {
msg = "Internal Server Error"
}
res := fiber.Map{
"status": "internal_server_error",
"message": msg,
}
return c.Status(fiber.StatusInternalServerError).JSON(res)
}
```

## KeyPairs Property

```go
type KeyPairs struct {
AESKeyForEncrypt []byte
AESIVForEncrypt []byte
AESKeyForDecrypt []byte
AESIVForDecrypt []byte
}
```

## Payload Template

An example of payload template (see more how to work in [Example](./example))

### Request

```json
{
"payload": "FDp1Dl31zGx5nRXFNKihB+k3ly/L7HI9tlHycbKVRwhaf3RRdyFGviuntEZqst0/"
}
```

which can be decrypt to:

```json
{
"firstname": "Chinnawat",
"lastname": "Chimdee"
}
```

### Response

```json
{
"payload": "tpkWPEI6F/nfgUjjtwyKSUf1erxPL6rQt8jG3RitQ1KpvRALfR5YAgQ0CXYkrwLfTid6VdK3SNlffuu/kvI7Hj7br0ur01TUFUWxQ9cl+8U="
}
```

which encrypted from:

```json
{
"firstname": "Chinnawat [Modified]",
"lastname": "Chimdee [Modified]"
}
```

Note: This payload using

- AESKeyForEncrypt (used in encrypting response body): `67890123456789012345678901234567`
- AESIVForEncrypt (used in encrypting response body): `6789012345678901`
- AESKeyForDecrypt (used in decrypting request body): `12345678901234567890123456789012`
- AESIVForDecrypt (used in decrypting request body): `1234567890123456`

## Example Usage

Please go to [example/README.md](./example/README.md)

## [NEW!] AES Encryption Tools (useful for debugging)

[https://encrypt-tools.vercel.app/](https://encrypt-tools.vercel.app/)