| 931 | } |
| 932 | |
| 933 | func (db *dbCrypt) encryptField(field *string, digest *sql.NullString) error { |
| 934 | // If no cipher is loaded, then we can't encrypt anything! |
| 935 | if db.ciphers == nil || db.primaryCipherDigest == "" { |
| 936 | return nil |
| 937 | } |
| 938 | |
| 939 | if field == nil { |
| 940 | return xerrors.Errorf("developer error: encryptField called with nil field") |
| 941 | } |
| 942 | if digest == nil { |
| 943 | return xerrors.Errorf("developer error: encryptField called with nil digest") |
| 944 | } |
| 945 | |
| 946 | encrypted, err := db.ciphers[db.primaryCipherDigest].Encrypt([]byte(*field)) |
| 947 | if err != nil { |
| 948 | return err |
| 949 | } |
| 950 | // Base64 is used to support UTF-8 encoding in PostgreSQL. |
| 951 | *field = b64encode(encrypted) |
| 952 | *digest = sql.NullString{String: db.primaryCipherDigest, Valid: true} |
| 953 | return nil |
| 954 | } |
| 955 | |
| 956 | // decryptFields decrypts the given field using the key with the given digest. |
| 957 | // If the value fails to decrypt, sql.ErrNoRows will be returned. |