Skip to content

Commit

Permalink
DecryptMarshal: json UseNumber (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
loopfz committed Aug 27, 2019
1 parent 0d66d1a commit 95035df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
19 changes: 11 additions & 8 deletions symmecrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package symmecrypt_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
Expand Down Expand Up @@ -102,8 +103,9 @@ func TestEncryptDecrypt(t *testing.T) {
}

type testObfuscate struct {
Name string
Amount int
Name string
Amount int
InterfaceNumber interface{}
}

func TestEncryptDecryptMarshal(t *testing.T) {
Expand All @@ -114,8 +116,9 @@ func TestEncryptDecryptMarshal(t *testing.T) {
}

origin := &testObfuscate{
Name: "test",
Amount: 10,
Name: "test",
Amount: 10,
InterfaceNumber: 2345678954,
}

extra := []byte("aa")
Expand Down Expand Up @@ -149,11 +152,11 @@ func TestEncryptDecryptMarshal(t *testing.T) {
t.Fatal("succerssfully decrypted cipher without using extra data -> ERROR")
}

if target.Name != origin.Name || target.Amount != origin.Amount {
t.Errorf("Not same deobfuscated result %s, %d", target.Name, target.Amount)
if target.Name != origin.Name || target.Amount != origin.Amount || fmt.Sprint(origin.InterfaceNumber) != fmt.Sprint(target.InterfaceNumber) {
t.Errorf("Not same deobfuscated result %s, %d, %v", target.Name, target.Amount, target.InterfaceNumber)
}
if targetExtra.Name != origin.Name || targetExtra.Amount != origin.Amount {
t.Errorf("Not same deobfuscated result %s, %d", targetExtra.Name, targetExtra.Amount)
if targetExtra.Name != origin.Name || targetExtra.Amount != origin.Amount || fmt.Sprint(origin.InterfaceNumber) != fmt.Sprint(targetExtra.InterfaceNumber) {
t.Errorf("Not same deobfuscated result %s, %d, %v", targetExtra.Name, targetExtra.Amount, targetExtra.InterfaceNumber)
}
}

Expand Down
5 changes: 4 additions & 1 deletion symutils/symutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package symutils

import (
"bytes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
Expand Down Expand Up @@ -202,7 +203,9 @@ func (b KeyAEAD) DecryptMarshal(s string, target interface{}, extra ...[]byte) e
if err != nil {
return err
}
return json.Unmarshal(unciphered, target)
dec := json.NewDecoder(bytes.NewReader(unciphered))
dec.UseNumber()
return dec.Decode(target)
}

// Wait is a noop for regular implementations: the key is always ready
Expand Down

0 comments on commit 95035df

Please sign in to comment.