// Encodes all data needed to decrypt message into a JSON string and writes it to disk.
7423
func writeCiphertext(salt []byte, keyiter int, isPass bool, sealed secret.SealedData, filename string) error {
	// fill in the ciphertext fields
	ec := EncodedCiphertext{
		CiphertextNonce: sealed.NonceBytes(),
		Ciphertext:      sealed.CiphertextBytes(),
		CipherAlgorithm: "salsa20_poly1305",
	}

	// if we used a passphrase, also set the passphrase fields
	if isPass == true {
		ec.KeySalt = salt
		ec.KeyIter = keyiter
		ec.KeyAlgorithm = "pbkdf#2"
	}

	// marshal encoded ciphertext into a json string
	b, err := json.MarshalIndent(ec, "", "   ")
	if err != nil {
		return err
	}

	// write to disk with read only permissions for the current user
	return ioutil.WriteFile(filename, b, 0600)
}
798030
func (e *encryption) Decrypt(data []byte) ([]byte, error) {
	length := len(data)
	if e.padding > 0 && length != e.padding {
		return nil, fmt.Errorf("Data length different than padding, data length %v padding %v", length, e.padding)
	}
	out := make([]byte, length)
	e.transform(data, out)
	return out, nil
}
1206269
func Encode(ctx context.Context) ([]byte, error) {
	m := ToMap(ctx)
	if len(m) > 0 {
		return json.Marshal(m)
	}
	return nil, nil
}
896095
func (enc *Encoder) writeBytes(p []byte) {
	enc.buf = append(enc.buf, p...)
}
792666
func (p *PreparedMsg) Encode(s Stream, msg interface{}) error {
	ctx := s.Context()
	rpcInfo, ok := rpcInfoFromContext(ctx)
	if !ok {
		return status.Errorf(codes.Internal, "grpc: unable to get rpcInfo")
	}

	// check if the context has the relevant information to prepareMsg
	if rpcInfo.preloaderInfo == nil {
		return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo is nil")
	}
	if rpcInfo.preloaderInfo.codec == nil {
		return status.Errorf(codes.Internal, "grpc: rpcInfo.preloaderInfo.codec is nil")
	}

	// prepare the msg
	data, err := encode(rpcInfo.preloaderInfo.codec, msg)
	if err != nil {
		return err
	}
	p.encodedData = data
	compData, err := compress(data, rpcInfo.preloaderInfo.cp, rpcInfo.preloaderInfo.comp)
	if err != nil {
		return err
	}
	p.hdr, p.payload = msgHeader(data, compData)
	return nil
}
132475
func (enc *WALEncoder) Encode(v *TimedWALMessage) error {
	data := cdc.MustMarshalBinaryBare(v)

	crc := crc32.Checksum(data, crc32c)
	length := uint32(len(data))
	if length > maxMsgSizeBytes {
		return fmt.Errorf("Msg is too big: %d bytes, max: %d bytes", length, maxMsgSizeBytes)
	}
	totalLength := 8 + int(length)

	msg := make([]byte, totalLength)
	binary.BigEndian.PutUint32(msg[0:4], crc)
	binary.BigEndian.PutUint32(msg[4:8], length)
	copy(msg[8:], data)

	_, err := enc.wr.Write(msg)

	return err
}
993488
func encodeSecret(sec *v1.Secret) (string, error) {
	// json.Marshal sorts the keys in a stable order in the encoding
	data, err := json.Marshal(map[string]interface{}{"kind": "Secret", "type": sec.Type, "name": sec.Name, "data": sec.Data})
	if err != nil {
		return "", err
	}
	return string(data), nil
}
1102564
func (jsonEncoder) Encode(v ...interface{}) string {
	var data interface{} = v
	if v == nil {
		// So that empty results produces `[]` and not `null`
		data = []interface{}{}
	} else if len(v) == 1 {
		data = v[0]
	}
	b, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}
	log.Printf("%s", b)
	return string(b)
}
1233363
func (l *LogUpdate) Encode(w io.Writer) error {
	return WriteElements(w, l.LogIndex, l.UpdateMsg)
}
769409
func (d DecrypterJSON) decryptJSON(data map[string]interface{}, decryptFunc DecryptFunc) error {
	for k, v := range data {
		switch t := v.(type) {
		case string:
			if s := strings.TrimPrefix(t, d.prefix); s != t {
				s, err := base64.URLEncoding.DecodeString(s)
				if err != nil {
					return err
				}

				decryptedData, err := decryptFunc(s)
				if err != nil {
					return err
				}

				data[k] = string(decryptedData)
			}
		case map[string]interface{}:
			err := d.decryptJSON(t, decryptFunc)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
1333880
func Encode(hash hash.Hash, msg, salt []byte) ([]byte, []byte, error) {
	if hash == nil {
		hash = sha256.New()
	}
	rand, err := rand.B.Alphanumeric(hash.Size())
	if err != nil {
		return nil, nil, err
	}

	return SaltEncode(hash, msg, salt, rand), rand, err
}
1347025
func (s *Session) Encode() ([]byte, error) {
	if len(s.data) == 0 {
		return nil, nil
	}

	if len(s.keys) == 0 {
		return nil, errors.New("at least one encryption key is required")
	}

	msg, err := msgpack.Marshal(s.data)
	if err != nil {
		return nil, errors.Wrapf(err, "failed encoding session data")
	}

	var nonce [24]byte
	if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
		return nil, errors.Wrapf(err, "failed generating nonce value")
	}

	var key [32]byte
	copy(key[:], s.keys[0])

	box := secretbox.Seal(nonce[:], msg, &nonce, &key)

	data := make([]byte, base64.RawStdEncoding.EncodedLen(len(box)))
	base64.RawStdEncoding.Encode(data, box)

	return data, nil
}
805945
func writeJSON(buf *bytes.Buffer, v interface{}, keyName string) error {
	if enc, err := json.Marshal(v); err != nil {
		return errors.Wrapf(err, `failed to encode '%s'`, keyName)
	} else {
		buf.Write(enc)
	}
	return nil
}
123078
func EncodeBytes(data []byte) string {
	mt := http.DetectContentType(data)
	// http.DetectContentType may add spurious spaces between ; and a parameter.
	// The canonical way is to not have them.
	cleanedMt := strings.Replace(mt, "; ", ";", -1)

	return New(data, cleanedMt).String()
}
661900
func (c *Config) EncryptBytes() ([]byte, error) {
	return base64.StdEncoding.DecodeString(c.EncryptKey)
}
508831
func (e encodable) MarshalJSON() ([]byte, error) {
	return Encode(e.E, e.obj)
}
1030335
func (r *Receipt) EncodeJSON() (string, error) {
	data, err := rlp.EncodeToBytes(r.receipt)
	return string(data), err
}