1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-23 19:59:26 +01:00
This commit is contained in:
Philipp Heckel 2022-07-05 14:15:14 -04:00
parent febe45818c
commit 99e6c0ff97
4 changed files with 41 additions and 1 deletions

View file

@ -8,6 +8,7 @@ import (
"errors"
"io"
)
import "gopkg.in/square/go-jose.v2"
const (
versionByte = 0x31 // "1"
@ -81,6 +82,30 @@ func Decrypt(input string, key []byte) (string, error) {
return string(plaintext), nil
}
func EncryptJWE(plaintext string, key []byte) (string, error) {
enc, err := jose.NewEncrypter(jose.A256GCM, jose.Recipient{Algorithm: jose.DIRECT, Key: key}, nil)
if err != nil {
return "", err
}
jwe, err := enc.Encrypt([]byte(plaintext))
if err != nil {
return "", err
}
return jwe.CompactSerialize()
}
func DecryptJWE(input string, key []byte) (string, error) {
jwe, err := jose.ParseEncrypted(input)
if err != nil {
return "", err
}
out, err := jwe.Decrypt(key)
if err != nil {
return "", err
}
return string(out), nil
}
func appendSlices(s ...[]byte) []byte {
var output []byte
for _, r := range s {

View file

@ -18,6 +18,16 @@ func TestEncryptDecrypt(t *testing.T) {
require.Equal(t, message, plaintext)
}
func TestEncryptDecryptJWE(t *testing.T) {
message := "this is a message or is it?"
ciphertext, err := EncryptJWE(message, []byte("AES256Key-32Characters1234567890"))
require.Nil(t, err)
plaintext, err := DecryptJWE(ciphertext, []byte("AES256Key-32Characters1234567890"))
require.Nil(t, err)
log.Println(ciphertext)
require.Equal(t, message, plaintext)
}
func TestEncryptExpectedOutputxxxxx(t *testing.T) {
// These values are taken from https://docs.pushbullet.com/#encryption
// The following expected ciphertext from the site was used as a baseline:

5
go.mod
View file

@ -25,7 +25,10 @@ require (
require github.com/pkg/errors v0.9.1 // indirect
require firebase.google.com/go/v4 v4.8.0
require (
firebase.google.com/go/v4 v4.8.0
gopkg.in/square/go-jose.v2 v2.6.0
)
require (
cloud.google.com/go v0.102.1 // indirect

2
go.sum
View file

@ -725,6 +725,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=