// NewID generates a new identifier for use where random identifiers with low // collision probability are required. // // With the parameters in this package, the generated identifier will provide // 128 bits of entropy encoded with base36. Leading padding is added if the // string is less 25 bytes. We do not intend to maintain this interface, so // identifiers should be treated opaquely. func NewID() string { var p [randomIDEntropyBytes]byte if _, err := io.ReadFull(idReader, p[:]); err != nil { panic(fmt.Errorf("failed to read random bytes: %v", err)) } var nn big.Int nn.SetBytes(p[:]) return fmt.Sprintf("%0[1]*s", maxRandomIDLength, nn.Text(randomIDBase)) }
func getCAHashFromToken(token string) (digest.Digest, error) { split := strings.Split(token, "-") if len(split) != 4 || split[0] != "SWMTKN" || split[1] != "1" { return "", errors.New("invalid join token") } var digestInt big.Int digestInt.SetString(split[2], joinTokenBase) return digest.ParseDigest(fmt.Sprintf("sha256:%0[1]*s", 64, digestInt.Text(16))) }
func generateRandomSecret() string { var secretBytes [generatedSecretEntropyBytes]byte if _, err := cryptorand.Read(secretBytes[:]); err != nil { panic(fmt.Errorf("failed to read random bytes: %v", err)) } var nn big.Int nn.SetBytes(secretBytes[:]) return fmt.Sprintf("%0[1]*s", maxGeneratedSecretLength, nn.Text(generatedSecretBase)) }
// GenerateJoinToken creates a new join token. func GenerateJoinToken(rootCA *RootCA) string { var secretBytes [generatedSecretEntropyBytes]byte if _, err := cryptorand.Read(secretBytes[:]); err != nil { panic(fmt.Errorf("failed to read random bytes: %v", err)) } var nn, digest big.Int nn.SetBytes(secretBytes[:]) digest.SetString(rootCA.Digest.Hex(), 16) return fmt.Sprintf("SWMTKN-1-%0[1]*s-%0[3]*s", base36DigestLen, digest.Text(joinTokenBase), maxGeneratedSecretLength, nn.Text(joinTokenBase)) }
func checkResult(t test, expr, key string, r *big.Int) { if t.Values[key].Cmp(r) != 0 { fmt.Fprintf(os.Stderr, "Line %d: %s did not match %s.\n\tGot %s\n", t.LineNumber, expr, key, r.Text(16)) } }
func (c *VFSCAStore) buildPrivateKeyPath(id string, serial *big.Int) vfs.Path { return c.basedir.Join("private", id, serial.Text(10)+".key") }
func (c *VFSCAStore) buildCertificatePath(id string, serial *big.Int) vfs.Path { return c.basedir.Join("issued", id, serial.Text(10)+".crt") }