// Crypto returns the crypto backend
977669
func (r *Store) Crypto(ctx context.Context, name string) backend.Crypto {
	_, sub, _ := r.getStore(ctx, name)
	if !sub.Valid() {
		out.Debug(ctx, "Sub-Store not found for %s. Returning nil crypto backend", name)
		return nil
	}
	return sub.Crypto()
}
362927
func (a *btcAddress) encrypt(key []byte) error {
	if a.flags.encrypted {
		return ErrAlreadyEncrypted
	}
	if len(a.privKeyCT) != 32 {
		return errors.New("invalid clear text private key")
	}

	aesBlockEncrypter, err := aes.NewCipher(key)
	if err != nil {
		return err
	}
	aesEncrypter := cipher.NewCFBEncrypter(aesBlockEncrypter, a.initVector[:])

	aesEncrypter.XORKeyStream(a.privKey[:], a.privKeyCT)

	a.flags.hasPrivKey = true
	a.flags.encrypted = true
	return nil
}
520788
func (con *Connection) getEncrypter() crypto.Encrypter {
	session := con.context.GetSessionForConnection(con.connection)
	if session != nil {
		return session.Encrypter()
	}

	return nil
}
1131513
func (priv *PrivateKey) Decrypt(rand io.Reader, ciphertext []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) {
	if opts == nil {
		return DecryptPKCS1v15(rand, priv, ciphertext)
	}

	switch opts := opts.(type) {
	case *OAEPOptions:
		return DecryptOAEP(opts.Hash.New(), rand, priv, ciphertext, opts.Label)

	case *PKCS1v15DecryptOptions:
		if l := opts.SessionKeyLen; l > 0 {
			plaintext = make([]byte, l)
			if _, err := io.ReadFull(rand, plaintext); err != nil {
				return nil, err
			}
			if err := DecryptPKCS1v15SessionKey(rand, priv, ciphertext, plaintext); err != nil {
				return nil, err
			}
			return plaintext, nil
		} else {
			return DecryptPKCS1v15(rand, priv, ciphertext)
		}

	default:
		return nil, errors.New("crypto/rsa: invalid options for Decrypt")
	}
}
556624
func (s *Action) Clone(ctx context.Context, c *cli.Context) error {
	if c.IsSet("crypto") {
		ctx = backend.WithCryptoBackendString(ctx, c.String("crypto"))
	}
	if c.IsSet("sync") {
		ctx = backend.WithRCSBackendString(ctx, c.String("sync"))
	}

	if len(c.Args()) < 1 {
		return ExitError(ctx, ExitUsage, nil, "Usage: %s clone repo [mount]", s.Name)
	}

	repo := c.Args()[0]
	mount := ""
	if len(c.Args()) > 1 {
		mount = c.Args()[1]
	}

	path := c.String("path")

	return s.clone(ctx, repo, mount, path)
}
988706
func encryptAction(client *gocd.Client, c *cli.Context) (r interface{}, resp *gocd.APIResponse, err error) {
	var value string
	if value = c.String("value"); value == "" {
		return nil, nil, NewFlagError("value")
	}

	return client.Encryption.Encrypt(context.Background(), value)
}
891229
func GenerateCryptoKey() (*CryptoKey, error) {
	var key CryptoKey
	_, err := io.ReadFull(prng, key[:])
	if err != nil {
		return nil, err
	}

	return &key, nil
}
582117
func ConfigFromBackend(coreBackend ...core.ConfigBackend) core.CryptoSuiteConfig {
	return &Config{backend: lookup.New(coreBackend...)}
}
687581
func detectCryptoBackend(ctx context.Context, path string) backend.CryptoBackend {
	if fsutil.IsFile(filepath.Join(path, xc.IDFile)) {
		return backend.XC
	}
	return backend.GPGCLI
}
1111204
func HasCryptoBackend(ctx context.Context) bool {
	_, ok := ctx.Value(ctxKeyCryptoBackend).(CryptoBackend)
	return ok
}
326657
func WithCryptoBackend(ctx context.Context, be CryptoBackend) context.Context {
	return context.WithValue(ctx, ctxKeyCryptoBackend, be)
}
832417
func GetCryptoBackend(ctx context.Context, cb backend.CryptoBackend, cfgdir string, agent *client.Client) (backend.Crypto, error) {
	ctx = client.WithClient(ctx, agent)
	ctx = ctxutil.WithConfigDir(ctx, cfgdir)
	crypto, err := backend.NewCrypto(ctx, cb)
	if err != nil {
		return nil, errors.Wrapf(err, "unknown crypto backend")
	}
	return crypto, nil
}
1088765
func (csp *CSP) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
	// Validate arguments
	if k == nil {
		return nil, errors.New("Invalid Key. It must not be nil.")
	}

	decryptor, found := csp.Decryptors[reflect.TypeOf(k)]
	if !found {
		return nil, errors.Errorf("Unsupported 'DecryptKey' provided [%v]", k)
	}

	plaintext, err = decryptor.Decrypt(k, ciphertext, opts)
	if err != nil {
		return nil, errors.Wrapf(err, "Failed decrypting with opts [%v]", opts)
	}

	return
}
249333
func String(s string, method crypto.Hash) (string, error) {
	h, err := createHash(method)
	if err != nil {
		return "", err
	}

	io.WriteString(h, s)

	return fmt.Sprintf("%x", h.Sum(nil)), nil
}
1157780
func Bytes(l int, dev string) ([]byte, error) {
	if l <= 0 {
		return nil, e.New(ErrInvalidLength)
	}
	switch dev {
	case "go":
		b := make([]byte, l)
		for i := 0; i < l; i++ {
			b[i] = byte(rand.Intn(255))
		}
		return b, nil
	case "go-crypto":
		b := make([]byte, l)
		n, err := io.ReadFull(crypto.Reader, b)
		if n != len(b) || err != nil {
			return nil, e.Forward(err)
		}
		return b, nil
	default:
		f, err := os.Open(dev)
		if err != nil {
			return nil, e.Forward(err)
		}
		b := make([]byte, l)
		f.Read(b)
		f.Close()
		return b, nil
	}
	panic("don't get here")
}
1264299
func WithCryptoSuiteConfig(cryptoConfigs ...interface{}) Option {
	return func(opts *options) error {
		c, err := cryptosuite.BuildCryptoSuiteConfigFromOptions(cryptoConfigs...)
		if err != nil {
			return err
		}
		opts.CryptoSuiteConfig = c
		return nil
	}
}
373244
func (engine *CryptoEngine) Decrypt(encryptedBytes []byte) (*message, error) {

	var err error
	msg := new(message)

	// convert the bytes to an encrypted message
	encryptedMessage, err := encryptedMessageFromBytes(encryptedBytes)
	if err != nil {
		return nil, err
	}

	decryptedMessageBytes, valid := secretbox.Open(nil, encryptedMessage.data, &encryptedMessage.nonce, &engine.secretKey)

	// if the verification failed
	if !valid {
		return nil, MessageDecryptionError
	}

	// means we successfully managed to decrypt
	msg, err = messageFromBytes(decryptedMessageBytes)
	return msg, nil

}