https://github.com/powerman/sensitive
Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration.
https://github.com/powerman/sensitive
go golang golang-library sensitive-data
Last synced: 8 months ago
JSON representation
Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration.
- Host: GitHub
- URL: https://github.com/powerman/sensitive
- Owner: powerman
- License: mit
- Created: 2020-11-08T20:46:18.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-10-27T22:34:45.000Z (about 3 years ago)
- Last Synced: 2025-05-06T03:58:07.492Z (8 months ago)
- Topics: go, golang, golang-library, sensitive-data
- Language: Go
- Homepage:
- Size: 114 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go package with base types protected from the human eye
[](https://pkg.go.dev/github.com/powerman/sensitive)
[](https://github.com/powerman/sensitive/actions?query=workflow%3ACI%2FCD)
[](https://coveralls.io/github/powerman/sensitive?branch=master)
[](https://goreportcard.com/report/github.com/powerman/sensitive)
[](https://github.com/powerman/sensitive/releases/latest)
**NOTE:** This project has been started as a fork of
https://github.com/go-playground/sensitive, but as upstream repo have no
activity since initial commit and several new features were added here
(see [Releases](https://github.com/powerman/sensitive/releases)) this repo
was detached and you can consider it an independent maintained alternative
for the upstream repo.
Package sensitive provides base types who's values should never be seen by
the human eye, but still used for configuration.
Sometimes you have a variable, such as a password, passed into your
program via arguments or ENV variables.
Some of these variables are very sensitive! and should not in any
circumstance be loggged or sent via JSON, despite JSON's "-", which people
may forget.
These variables, which are just typed primitive types, have their
overridden `fmt.Formatter`, `encoding.MarshalText` & `json.Marshal`
implementations.
As an added bonus using them as their base type eg. String => string, you
have to explicitly cast the eg. string(s) This makes you think about what
you're doing and why you casting it providing additional safety.
Supported types:
- `Bool`
- `Bytes`
- `Decimal` (https://github.com/shopspring/decimal)
- `Float32`
- `Float64`
- `Int`
- `Int8`
- `Int16`
- `Int32`
- `Int64`
- `String` (the most useful)
- `Uint`
- `Uint8`
- `Uint16`
- `Uint32`
- `Uint64`
## Examples
### Basic
```go
// go run _examples/basic/main.go mypassword
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/powerman/sensitive"
)
func main() {
password := sensitive.String(os.Args[1])
fmt.Printf("%s\n", password)
fmt.Printf("%v\n", password)
b, _ := json.Marshal(password)
fmt.Println(string(b))
var empty *sensitive.String
b, _ = json.Marshal(empty)
fmt.Println(string(b))
// output:
//
//
// ""
// null
}
```
### Custom Formatting
```go
// go run _examples/custom/main.go mypassword
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/powerman/sensitive"
)
func init() {
// override default Formatter
sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) {
switch c {
default:
sensitive.Format(f, c, "redacted")
case 'v':
sensitive.Format(f, c, string(s)[:4]+"*******")
}
}
}
func main() {
password := sensitive.String(os.Args[1])
fmt.Printf("%s\n", password)
fmt.Printf("%v\n", password)
b, _ := json.Marshal(password)
fmt.Println(string(b))
var empty *sensitive.String
b, _ = json.Marshal(empty)
fmt.Println(string(b))
// output:
// redacted
// mypa*******
// "mypa*******"
// null
}
```