@@ -7,22 +7,20 @@ package secret
77// Text provides a way to safely store your secret value and a corresponding redact hint. This
88// redact hint is what is used in operations like printing and serializing.
99type Text struct {
10- // v is the actual secret values.
11- v * string
12- // r is the redact hint to be used in place of secret.
13- r * string
10+ secret * string
11+ redact * string
1412}
1513
1614// New returns [Text] for the secret with [FiveStar] as the default redact hint. Provide options
1715// like [RedactHint] to modify default behavior.
1816func New (secret string , options ... func (* Text )) Text {
1917 tx := Text {
20- v : new (string ),
21- r : new (string ),
18+ secret : new (string ),
19+ redact : new (string ),
2220 }
2321
24- * tx .v = secret
25- * tx .r = FiveStar
22+ * tx .secret = secret
23+ * tx .redact = FiveStar
2624
2725 for _ , o := range options {
2826 o (& tx )
@@ -42,25 +40,25 @@ const (
4240// the common redact hints provided with this package like [FiveX] or provide your own string.
4341func RedactHint (r string ) func (* Text ) {
4442 return func (t * Text ) {
45- * t .r = r
43+ * t .redact = r
4644 }
4745}
4846
4947// String implements the [fmt.Stringer] interface and returns only the redact hint. This prevents the
5048// secret value from being printed to std*, logs etc.
5149func (tx Text ) String () string {
52- if tx .r == nil {
50+ if tx .redact == nil {
5351 return FiveStar
5452 }
55- return * tx .r
53+ return * tx .redact
5654}
5755
5856// Value gives you access to the actual secret value stored inside Text.
5957func (tx Text ) Value () string {
60- if tx .v == nil {
58+ if tx .secret == nil {
6159 return ""
6260 }
63- return * tx .v
61+ return * tx .secret
6462}
6563
6664// MarshalText implements [encoding.TextMarshaler]. It marshals redact string into bytes rather than
@@ -75,15 +73,15 @@ func (tx *Text) UnmarshalText(b []byte) error {
7573 s := string (b )
7674
7775 // If the original redact is not nil then use it otherwise fallback to default.
78- if tx .r != nil {
79- * tx = New (s , RedactHint (* tx .r ))
76+ if tx .redact != nil {
77+ * tx = New (s , RedactHint (* tx .redact ))
8078 } else {
8179 * tx = New (s )
8280 }
8381 return nil
8482}
8583
86- // Equals checks whether s2 has same secret string or not.
87- func ( tx * Text ) Equals ( s2 Text ) bool {
88- return * tx . v == * s2 . v
84+ // Equal returns true if both arguments have the same secret. The redact strings are not considered .
85+ func Equal ( tx1 , tx2 Text ) bool {
86+ return * tx1 . secret == * tx2 . secret
8987}
0 commit comments