Skip to content

Instantly share code, notes, and snippets.

@DannyHinshaw
Last active March 22, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DannyHinshaw/6baadf8e61b38ad05a76bcfcadb6583f to your computer and use it in GitHub Desktop.
Save DannyHinshaw/6baadf8e61b38ad05a76bcfcadb6583f to your computer and use it in GitHub Desktop.
// crypto.go
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
)
func EncryptPassword(key []byte, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
fmt.Printf("%s\n", b)
cipherText := make([]byte, aes.BlockSize+len(b))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(b))
return cipherText, nil
}
func DecryptPassword(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return nil, err
}
log.Println("DATA::", data)
return data, nil
}
// ComparePasswords - Decode, decrypt and compare passwords.
func ComparePasswords(key []byte, password1 string, password2 string) bool {
passDecoded1, err := hex.DecodeString(password1)
if err != nil {
log.Println(err)
return false
}
passDecoded2, err := hex.DecodeString(password2)
if err != nil {
log.Println(err)
return false
}
pass1, err := DecryptPassword(key, []byte(passDecoded1))
if err != nil {
log.Println(err)
return false
}
pass2, err := DecryptPassword(key, []byte(passDecoded2))
if err != nil {
log.Println(err)
return false
}
return string(pass1) == string(pass2)
}
// main.go
package main
import (
"context"
"encoding/hex"
"go-hybrid-alert/api/db"
"go-hybrid-alert/api/schemas"
"go-hybrid-alert/api/session"
"go-hybrid-alert/util/crypto"
"log"
)
func main() {
userId := "danny@test.com"
password := []byte("SECRET SAUCE!")
key := session.GetPrivateKey()
enc, err := crypto.EncryptPassword(key, password)
if err != nil {
log.Println("error encrypting: ", err)
}
log.Println(string(enc))
dec, err := crypto.DecryptPassword(key, enc)
if err != nil {
log.Println("error decrypting: ", err)
}
log.Println(dec)
// Add the new user/pass to firestore
ctx := context.Background()
user := schemas.FSUser{Password: string(enc)}
if _, err := db.UsersRef.Doc(userId).Set(ctx, user); err != nil {
log.Fatal(err)
}
}
// Output
// U0VDUkVUIFNBVUNFIQ==
// !��`0C��Z���Ta'�-�F�p?�y��7C}/E�#
// DATA:: [83 69 67 82 69 84 32 83 65 85 67 69 33]
// [83 69 67 82 69 84 32 83 65 85 67 69 33]
// rpc error: code = Internal desc = grpc: error while marshaling: proto: field "google.firestore.v1.Value.ValueType" contains invalid UTF-8
// exit status 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment