Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
Err = errors.New("ghoststring error")
)
Functions ¶
This section is empty.
Types ¶
type GhostString ¶
GhostString wraps a string with a JSON marshaller that uses a namespace-scoped encrypting Ghostifyer registered via SetGhostifyer
Example ¶
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"time"
"github.com/rstudio/ghoststring"
)
func main() {
if _, err := ghoststring.SetGhostifyer(
"example",
"correct horse battery staple",
); err != nil {
panic(err)
}
type DiaryEntry struct {
Timestamp time.Time `json:"timestamp"`
Text ghoststring.GhostString `json:"text"`
}
type Diary struct {
Author string `json:"author"`
Entries []DiaryEntry `json:"entries"`
}
enc, err := json.MarshalIndent(
&Diary{
Author: "Eagerly Anticipated",
Entries: []DiaryEntry{
{
Timestamp: time.UnixMicro(4),
Text: ghoststring.GhostString{
Namespace: "example",
String: "Nights without you are so dark. I pray that someday you will return my flashlight.",
},
},
{
Timestamp: time.UnixMicro(-8001),
Text: ghoststring.GhostString{
Namespace: "unknown",
String: "We may never know.",
},
},
},
},
"",
" ",
)
if err != nil {
panic(err)
}
encString := string(enc)
if strings.Contains(encString, "We may never know.") || strings.Contains(encString, "Nights without you") {
panic("not ghostly enough: contains cleartext")
}
if !strings.Contains(encString, `"text": ""`) {
panic("not ghostly enough: lacking empty ghoststring")
}
if matched, err := regexp.MatchString(`.+"text": "👻:[^"]+"`, encString); !matched || err != nil {
panic("not ghostly enough: lacking non-empty ghoststring")
}
fmt.Println("no peeking")
}
Output: no peeking
func (*GhostString) Equal ¶
func (gs *GhostString) Equal(other *GhostString) bool
Equal compares this GhostString to another
func (*GhostString) IsValid ¶
func (gs *GhostString) IsValid() bool
IsValid checks that the wrapped string value is non-empty and the namespace is valid
func (*GhostString) MarshalJSON ¶
func (gs *GhostString) MarshalJSON() ([]byte, error)
MarshalJSON allows GhostString to fulfill the json.Marshaler interface. The lack of a namespace is considered an error.
func (*GhostString) UnmarshalJSON ¶
func (gs *GhostString) UnmarshalJSON(b []byte) error
UnmarshalJSON allows GhostString to fulfill the json.Unmarshaler interface. The bytes are first unmarshaled as a string and then if non-empty are passed through an "unghostify" step.
type Ghostifyer ¶
type Ghostifyer interface {
Ghostify(*GhostString) (string, error)
Unghostify(string) (*GhostString, error)
}
Ghostifyer encrypts and encodes a *GhostString into a string representation that is acceptable for inclusion in JSON. The structure of a ghostified string is:
{prefix}base64({nonce}{namespace}{namespace separator}{value})
func SetGhostifyer ¶
func SetGhostifyer(namespace, key string) (Ghostifyer, error)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
ghoststring
command
|
|
|
internal
|
|
|
cmd/myths
command
|
|
|
cmd/rectangles
command
|