redact

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2023 License: MIT Imports: 2 Imported by: 1

README

Redact

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

A simple library to redact sensitive data.

Prerequisites

  • Go >= 1.18

Install

go get go.nhat.io/redact

Usage

package main

import (
	"fmt"

	"go.nhat.io/redact"
)

func ExampleValues() {
	r := redact.Values("hello")

	result := r.Redact("hello world!", "hello there")

	for _, s := range result {
		fmt.Println(s)
	}

	// Output:
	// ****** world!
	// ****** there
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package redact provides a simple way to redact sensitive information.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Environ = fn{func(values ...string) []string {
	env := os.Environ()
	oldNew := make([]string, 0, len(env)*2)

	for _, value := range env {
		name, _, _ := strings.Cut(value, "=")

		if v := os.Getenv(name); v != "" {
			oldNew = append(oldNew, v, defaultMask)
		}
	}

	return Redact(strings.NewReplacer(oldNew...).Replace, values)
}}

Environ redacts values that contain environment variables.

View Source
var NoRedact = fn{func(values ...string) []string {
	return values
}}

NoRedact does not redact values.

Functions

func Redact

func Redact(redact func(s string) string, values []string) []string

Redact redacts values.

Types

type Fn

type Fn func(values ...string) []string

Fn is a function that redacts values.

func (Fn) Redact

func (f Fn) Redact(values ...string) []string

Redact redacts values.

type Redactor

type Redactor interface {
	Redact(values ...string) []string
}

Redactor is an interface that redacts values.

func Use

func Use(redactors ...Redactor) Redactor

Use creates a new Redactor from a list of Redactors.

Example
package main

import (
	"fmt"
	"os"

	"go.nhat.io/redact"
)

func main() {
	_ = os.Setenv("CUSTOM_ENV", "world") //nolint: errcheck

	r := redact.Use(
		redact.Environ,
		redact.Values("hello"),
	)

	result := r.Redact("hello world!", "hello there")

	for _, s := range result {
		fmt.Println(s)
	}

}
Output:

****** ******!
****** there

type Redactors

type Redactors []Redactor

Redactors is a list of redactors.

func (Redactors) Redact

func (rs Redactors) Redact(values ...string) []string

Redact redacts values.

type Replacer

type Replacer func(s string) string

Replacer is a string replacer that redacts values.

func NewReplacer

func NewReplacer(mask string, values []string) Replacer

NewReplacer creates a new Replacer that replaces the sensitive values with the mask.

func Values

func Values(sensitiveValues ...string) Replacer

Values redacts the values.

Example
package main

import (
	"fmt"

	"go.nhat.io/redact"
)

func main() {
	r := redact.Values("hello")

	result := r.Redact("hello world!", "hello there")

	for _, s := range result {
		fmt.Println(s)
	}

}
Output:

****** world!
****** there

func (Replacer) Redact

func (r Replacer) Redact(values ...string) []string

Redact redacts values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL