|
| 1 | +## Package sensitive |
| 2 | + |
| 3 | +<img align="right" src="https://raw.githubusercontent.com/go-playground/sensitive/master/logo.jpg"> |
| 4 | +[](https://travis-ci.org/go-playground/sensitive) |
| 5 | +[](https://godoc.org/github.com/go-playground/sensitive) |
| 6 | + |
| 7 | + |
| 8 | +Package sensitive provides base types who's values should never be seen by the human eye, but still used for configuration. |
| 9 | + |
| 10 | +What? Explain |
| 11 | + |
| 12 | +Sometimes you have a variable, such as a password, passed into your program via arguments or ENV variables. |
| 13 | +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. |
| 14 | +These variables, which are just typed primitive types, have their overridden `fmt.Formatter`, `encoding.MarshalText` & `json.Marshal` implementations. |
| 15 | + |
| 16 | +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 safelty. |
| 17 | + |
| 18 | +Variables: |
| 19 | +- `String` - The most useful |
| 20 | +- `Bool` |
| 21 | +- `Float32` |
| 22 | +- `Float64` |
| 23 | +- `Int` |
| 24 | +- `Int8` |
| 25 | +- `Int16` |
| 26 | +- `Int32` |
| 27 | +- `Int64` |
| 28 | +- `Uint` |
| 29 | +- `Uint8` |
| 30 | +- `Uint16` |
| 31 | +- `Uint32` |
| 32 | +- `Uint64` |
| 33 | + |
| 34 | +Example |
| 35 | +------- |
| 36 | +```go |
| 37 | +// go run _examples/basic/main.go mypassword |
| 38 | +package main |
| 39 | + |
| 40 | +import ( |
| 41 | + "encoding/json" |
| 42 | + "fmt" |
| 43 | + "os" |
| 44 | + |
| 45 | + "github.com/go-playground/sensitive" |
| 46 | +) |
| 47 | + |
| 48 | +func main() { |
| 49 | + password := sensitive.String(os.Args[1]) |
| 50 | + |
| 51 | + fmt.Printf("%s\n", password) |
| 52 | + fmt.Printf("%v\n", password) |
| 53 | + |
| 54 | + b, _ := json.Marshal(password) |
| 55 | + fmt.Println(string(b)) |
| 56 | + |
| 57 | + var empty *sensitive.String |
| 58 | + b, _ = json.Marshal(empty) |
| 59 | + fmt.Println(string(b)) |
| 60 | + |
| 61 | + // output: |
| 62 | + // |
| 63 | + // |
| 64 | + // "" |
| 65 | + // null |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Custom Formatting |
| 70 | +----------------- |
| 71 | +```go |
| 72 | +package main |
| 73 | + |
| 74 | +import ( |
| 75 | + "encoding/json" |
| 76 | + "fmt" |
| 77 | + "io" |
| 78 | + "os" |
| 79 | + |
| 80 | + "github.com/go-playground/sensitive" |
| 81 | +) |
| 82 | + |
| 83 | +func init() { |
| 84 | + // override default Formatter |
| 85 | + sensitive.FormatStringFn = func(s sensitive.String, f fmt.State, c rune) { |
| 86 | + switch c { |
| 87 | + case 's': |
| 88 | + _, _ = io.WriteString(f, "redacted") |
| 89 | + case 'v': |
| 90 | + _, _ = io.WriteString(f, string(s)[:4]+"*******") |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func main() { |
| 96 | + password := sensitive.String(os.Args[1]) |
| 97 | + |
| 98 | + fmt.Printf("%s\n", password) |
| 99 | + fmt.Printf("%v\n", password) |
| 100 | + |
| 101 | + b, _ := json.Marshal(password) |
| 102 | + fmt.Println(string(b)) |
| 103 | + |
| 104 | + var empty *sensitive.String |
| 105 | + b, _ = json.Marshal(empty) |
| 106 | + fmt.Println(string(b)) |
| 107 | + |
| 108 | + // output: |
| 109 | + // redacted |
| 110 | + // mypa******* |
| 111 | + // "redacted" |
| 112 | + // null |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +License |
| 117 | +------ |
| 118 | +Distributed under MIT License, please see license file in code for more details. |
0 commit comments