Skip to content
Snippets Groups Projects
cryptutils.go 1.44 KiB
Newer Older
  • Learn to ignore specific revisions
  • Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    package cryptutils
    
    import (
    	"encoding/base64"
    	"os/exec"
    	"strings"
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    
    
    	"github.com/Songmu/prompter"
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    
    	"github.com/zachmann/mytoken/internal/utils/cryptUtils"
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    )
    
    // EncryptGPG encrypts the given string using the gpg key with the given id
    func EncryptGPG(str, id string) (string, error) {
    	cmd := exec.Command("/usr/bin/gpg", "-e", "--always-trust", "-r", id, "--armor")
    	cmd.Stdin = strings.NewReader(str)
    	out, err := cmd.Output()
    	if err != nil {
    		return "", err
    	}
    	encoded := base64.StdEncoding.EncodeToString(out)
    	return encoded, nil
    }
    
    // DecryptGPG decrypts the given ciphertext using the gpg key with the given id
    func DecryptGPG(ciph, id string) (string, error) {
    	cmd := exec.Command("/usr/bin/gpg", "-d", "-u", id)
    	decoded, err := base64.StdEncoding.DecodeString(ciph)
    	if err != nil {
    		return "", err
    	}
    	cmd.Stdin = strings.NewReader(string(decoded))
    	out, err := cmd.Output()
    	if err != nil {
    		return "", err
    	}
    	return string(out), nil
    }
    
    // EncryptPassword encrypts the given string using a password which the user is prompted for
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    func EncryptPassword(str string) (string, error) {
    
    	password := prompter.Password("Enter encryption password")
    	return cryptUtils.AES256Encrypt(str, password)
    
    // DecryptPassword decrypts the given string using a password which the user is prompted for
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    func DecryptPassword(ciph string) (string, error) {
    
    	password := prompter.Password("Enter decryption password")
    	return cryptUtils.AES256Decrypt(ciph, password)
    
    Gabriel Zachmann's avatar
    Gabriel Zachmann committed
    }