Пример #1
0
func encrypt(me *kindi.Identity, recipientEmails, filename, server string) error {
	certs, missing, err := fetchCertificates(server, recipientEmails)
	if err != nil {
		return err
	}

	if len(certs) == 0 {
		fmt.Fprintf(os.Stderr, "----------------------------------------------------\n")
		fmt.Fprintf(os.Stderr, "Warning: Couldn't find certificates for the following recipients: [%v]\n", recipientEmails)
		fmt.Fprintf(os.Stderr, "Those recipients will not be able to decrypt the file.\n")
		fmt.Fprintf(os.Stderr, "Please go to https://kindimonster.appspot.com/invite and invite them to join kindi.\n")
		fmt.Fprintf(os.Stderr, "----------------------------------------------------\n")

		return errors.New("no certificates fetched")
	}

	if len(missing) > 0 {
		fmt.Fprintf(os.Stderr, "----------------------------------------------------\n")
		fmt.Fprintf(os.Stderr, "Warning: Couldn't find certificates for the following recipients: %v\n", missing)
		fmt.Fprintf(os.Stderr, "Those recipients will not be able to decrypt the file.\n")
		fmt.Fprintf(os.Stderr, "Please go to https://kindimonster.appspot.com/invite and invite them to join kindi.\n")
		fmt.Fprintf(os.Stderr, "----------------------------------------------------\n")
	}

	r, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer r.Close()

	w, err := os.OpenFile(filename+".kindi", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return err
	}
	defer w.Close()

	_, err = w.Write([]byte(fileIdentifier))
	if err != nil {
		return err
	}

	sig, err := me.Sign()
	if err != nil {
		return err
	}

	metadata := KindiMetadata{
		Filename:        filepath.Base(filename),
		SenderEmail:     me.Email,
		SenderSignature: sig,
	}
	jsonMetadata, err := json.Marshal(metadata)
	if err != nil {
		return err
	}

	return me.Encrypt(w, jsonMetadata, r, certs)
}