// writeKeyValuePair write a key value pair to io.Writer
195514
func writeKeyValuePair(w io.Writer, key, value string) error {
	_, err := fmt.Fprintf(w, "%s:%s\n", key, value)
	return err
}
1095496
func WriteCertAndKey(pkiPath string, name string, cert *x509.Certificate, key crypto.Signer) error {
	if err := WriteKey(pkiPath, name, key); err != nil {
		return errors.Wrap(err, "couldn't write key")
	}

	return WriteCert(pkiPath, name, cert)
}
134768
func GenerateKeypair() (pub string, priv string, err error) {
	var kp crypto.Keypair
	if err := kp.Generate(); err != nil {
		return "", "", err
	}
	return kp.PublicString(), kp.PrivateString(), nil
}
558349
func (f Format) ValueForKey(key FormatKey) interface{} {
	switch key {
	case FormatItagKey:
		return f.Itag
	case FormatExtensionKey:
		return f.Extension
	case FormatResolutionKey:
		return f.Resolution
	case FormatVideoEncodingKey:
		return f.VideoEncoding
	case FormatAudioEncodingKey:
		return f.AudioEncoding
	case FormatAudioBitrateKey:
		return f.AudioBitrate
	default:
		if f.meta != nil {
			return f.meta[string(key)]
		}
		return nil
	}
}
812613
func (e *Engine) WriteValues(values map[string][]Value) error {
	e.mu.RLock()
	defer e.mu.RUnlock()

	if err := e.Cache.WriteMulti(values); err != nil {
		return err
	}

	return nil
}
908034
func (c *agency) WriteKey(ctx context.Context, key []string, value interface{}, ttl time.Duration, condition ...WriteCondition) error {
	var cond WriteCondition
	switch len(condition) {
	case 0:
	// No condition, do nothing
	case 1:
		cond = condition[0]
	default:
		return driver.WithStack(fmt.Errorf("too many conditions"))
	}
	if err := c.write(ctx, "set", key, value, cond, ttl); err != nil {
		return driver.WithStack(err)
	}
	return nil
}
1149986
func (d *ConsulStateDriver) Write(key string, value []byte) error {
	key = processKey(key)

	var err error

	for i := 0; i < maxConsulRetries; i++ {
		_, err = d.Client.KV().Put(&api.KVPair{Key: key, Value: value}, nil)
		if err != nil && (api.IsServerError(err) || strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "connection refused")) {
			// Retry after a delay
			time.Sleep(time.Second)
			continue
		}

		// when err == nil or anything other than connection refused
		return err
	}

	// err could be anything other than connection refused
	return err
}
1145521
func KeyPairFor(host, commonName, pkfile, certfile string) (tls.Certificate, error) {
	mypkfile := pkfile
	if mypkfile == "" {
		mypkfile = "key.pem"
	}
	mycertfile := certfile
	if mycertfile == "" {
		mycertfile = "cert.pem"
	}
	ctx := certContext{
		PKFile:         mypkfile,
		ServerCertFile: mycertfile,
	}
	_, err1 := os.Stat(ctx.ServerCertFile)
	_, err2 := os.Stat(ctx.PKFile)
	if os.IsNotExist(err1) || os.IsNotExist(err2) {
		fmt.Println("At least one of the Key/Cert files is not found -> Generating new key pair")
		err := ctx.initPKAndCert(host, commonName)
		if err != nil {
			return tls.Certificate{}, fmt.Errorf("Unable to init server cert: %s\n", err)
		}
	}

	cert, err := tls.LoadX509KeyPair(ctx.ServerCertFile, ctx.PKFile)
	if err != nil {
		return tls.Certificate{}, fmt.Errorf("Unable to load certificate and key from %s and %s: %s\n", ctx.ServerCertFile, ctx.PKFile, err)
	}
	return cert, err
}
157624
func (k *Keygen) GenerateKeyPair(passphrase string) ([]byte, []byte, error) {
	return GenerateKeyPair(passphrase)
}
1062459
func writeKey(filename, pathFormat string, data []byte) error {
	dst := make([]byte, hex.EncodedLen(len(data))) //StdEncoding.EncodedLen(len(data)))
	hex.Encode(dst, data)                          // StdEncoding.Encode(dst, data)
	filePath := fmt.Sprintf(pathFormat, filename)
	return writeFile(filePath, dst)
}
687629
func StoreKeysIncompat(k I2PKeys, w io.Writer) (err error) {
	_, err = io.WriteString(w, k.addr.Base64()+"\n"+k.both)
	return
}
587227
func (s *CreateServerInput) SetKeyPair(v string) *CreateServerInput {
	s.KeyPair = &v
	return s
}
400112
func (c *agency) WriteKeyIfEqualTo(ctx context.Context, key []string, newValue, oldValue interface{}, ttl time.Duration) error {
	var cond WriteCondition
	cond = cond.IfEqualTo(key, oldValue)
	if err := c.write(ctx, "set", key, newValue, cond, ttl); err != nil {
		return driver.WithStack(err)
	}
	return nil
}
1203773
func (c *Certificate) WriteKeyPEM(out io.Writer) error {
	_, err := out.Write(c.KeyPEM)
	return errors.Trace(err)
}
715399
func (b *recordBuffer) WriteDashOrIntValue(v int) {
	if v == 0 {
		_, _ = b.WriteString(`"-"`)
		b.writeSpace()
	} else {
		b.WriteIntValue(v)
	}
}
116484
func GenerateKeyPair(passphrase string) ([]byte, []byte, error) {
	priv, err := rsa.GenerateKey(rand.Reader, teleport.RSAKeySize)
	if err != nil {
		return nil, nil, err
	}
	privDer := x509.MarshalPKCS1PrivateKey(priv)
	privBlock := pem.Block{
		Type:    "RSA PRIVATE KEY",
		Headers: nil,
		Bytes:   privDer,
	}
	privPem := pem.EncodeToMemory(&privBlock)

	pub, err := ssh.NewPublicKey(&priv.PublicKey)
	if err != nil {
		return nil, nil, err
	}
	pubBytes := ssh.MarshalAuthorizedKey(pub)
	return privPem, pubBytes, nil
}
548472
func (e *Engine) GenerateKeyPairs(ctx context.Context) (*KeyPairs, error) {
	kp := &KeyPairs{}

	err := ctxutil.ErrIfDone(ctx)
	if err != nil {
		return nil, err
	}

	pubSig, privSig, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		return nil, err
	}

	sealedSig, nonceSig, err := e.Seal(ctx, privSig)
	if err != nil {
		return nil, err
	}

	kp.Signature.Private = sealedSig
	kp.Signature.Public = pubSig
	kp.Signature.PNonce = nonceSig

	err = ctxutil.ErrIfDone(ctx)
	if err != nil {
		return nil, err
	}

	pubEnc, privEnc, err := box.GenerateKey(rand.Reader)
	if err != nil {
		return nil, err
	}

	sealedEnc, nonceEnc, err := e.Seal(ctx, (*privEnc)[:])
	if err != nil {
		return nil, err
	}

	kp.Encryption.Private = sealedEnc
	kp.Encryption.Public = *pubEnc
	kp.Encryption.PNonce = nonceEnc

	return kp, nil
}