From 65f36de79e02a0644467a5d1ac3ce18ba7934a7b Mon Sep 17 00:00:00 2001 From: John Brooks Date: Sun, 11 Sep 2016 21:37:48 -0600 Subject: [PATCH] core: Add forgotten utils/misc.go file --- core/utils/misc.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 core/utils/misc.go diff --git a/core/utils/misc.go b/core/utils/misc.go new file mode 100644 index 0000000..6c95c23 --- /dev/null +++ b/core/utils/misc.go @@ -0,0 +1,61 @@ +package utils + +import ( + "crypto/rsa" + "errors" + "github.com/yawning/bulb/utils/pkcs1" + "strings" +) + +// Take a string containing substrings separated by sep, and +// return a slice of substrings as in strings.Split. Double quotes +// are stripped from the output, and separator characters within +// double quotes are included in the substring verbatim. Quotes and +// escape characters can be escaped with a preceding backslash, which +// is stripped from the output. +func UnquoteStringSplit(s string, sep rune) []string { + var re []string + var quoted bool + var escaped bool + var current string + + for _, c := range s { + if c == '"' && !escaped { + quoted = !quoted + } else if c == '\\' && !escaped { + escaped = true + } else if c == sep && !quoted && !escaped { + re = append(re, current) + current = "" + } else { + current += string(c) + escaped = false + } + } + + return append(re, current) +} + +func RicochetAddressFromKey(key *rsa.PublicKey) (string, error) { + addr, err := pkcs1.OnionAddr(key) + if err != nil { + return "", err + } else if addr == "" { + return "", errors.New("Invalid key") + } + return "ricochet:" + addr, nil +} + +func RicochetAddressFromOnion(onion string) (string, error) { + if len(onion) != 23 || !strings.HasSuffix(onion, ".onion") { + return "", errors.New("Invalid onion address") + } + return "ricochet:" + onion[:16], nil +} + +func OnionFromRicochetAddress(address string) (string, error) { + if len(address) != 25 || !strings.HasPrefix(address, "ricochet:") { + return "", errors.New("Invalid ricochet address") + } + return address[9:] + ".onion", nil +}