-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pair: make hashing deterministic regardless of orders (#76)
- Loading branch information
Showing
2 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,73 @@ func TestPAIR(t *testing.T) { | |
} | ||
} | ||
|
||
func TestDeterministicEncryption(t *testing.T) { | ||
var ( | ||
salt = make([]byte, sha256SaltSize) | ||
scalar = ristretto255.NewScalar() | ||
) | ||
|
||
if _, err := rand.Read(salt); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// sha512 produces a 64-byte psuedo-uniformized data | ||
src := sha512.Sum512(salt) | ||
scalar.FromUniformBytes(src[:]) | ||
sk, err := scalar.MarshalText() | ||
if err != nil { | ||
t.Fatalf("failed to marshal the scalar: %s", err.Error()) | ||
} | ||
|
||
// Create a new PAIR instance | ||
pairID := PAIRSHA256Ristretto255 | ||
|
||
pair, err := pairID.New(salt, sk) | ||
if err != nil { | ||
t.Fatalf("failed to instantiate a new PAIR instance: %s", err.Error()) | ||
} | ||
|
||
var ( | ||
id1 = []byte("[email protected]") | ||
id2 = []byte("[email protected]") | ||
) | ||
|
||
// Encrypt the data | ||
ciphertext1, err := pair.Encrypt(id1) | ||
if err != nil { | ||
t.Fatalf("failed to encrypt the data: %s", err.Error()) | ||
} | ||
|
||
ciphertext2, err := pair.Encrypt(id2) | ||
if err != nil { | ||
t.Fatalf("failed to encrypt the data: %s", err.Error()) | ||
} | ||
|
||
// reverse order | ||
reversePair, err := pairID.New(salt, sk) | ||
if err != nil { | ||
t.Fatalf("failed to instantiate a new PAIR instance: %s", err.Error()) | ||
} | ||
|
||
ciphertext3, err := reversePair.Encrypt(id2) | ||
if err != nil { | ||
t.Fatalf("failed to encrypt the data: %s", err.Error()) | ||
} | ||
|
||
ciphertext4, err := reversePair.Encrypt(id1) | ||
if err != nil { | ||
t.Fatalf("failed to encrypt the data: %s", err.Error()) | ||
} | ||
|
||
if strings.Compare(string(ciphertext1), string(ciphertext4)) != 0 { | ||
t.Fatalf("want: %s, got: %s", string(ciphertext1), string(ciphertext4)) | ||
} | ||
|
||
if strings.Compare(string(ciphertext2), string(ciphertext3)) != 0 { | ||
t.Fatalf("want: %s, got: %s", string(ciphertext2), string(ciphertext3)) | ||
} | ||
} | ||
|
||
func genData(n int) [][]byte { | ||
data := make([][]byte, n) | ||
for i := 0; i < n; i++ { | ||
|