Skip to content
Snippets Groups Projects
Commit 21898bb1 authored by Josh Brooks's avatar Josh Brooks
Browse files

Small refactor of method names in timestamp.go

parent 163060c4
No related branches found
No related tags found
1 merge request!6Release
......@@ -18,36 +18,36 @@ import (
// This file handles signature and verification logic of the timestamp for a user's verification.
// This is used to verify that a user has registered with the network at a specific data and time
// SignTimestamp signs a hash of the timestamp and the user's public key
func SignTimestamp(rand io.Reader, priv *rsa.PrivateKey, ts time.Time,
// SignWithTimestamp signs a hash of the timestamp and the user's public key
func SignWithTimestamp(rand io.Reader, priv *rsa.PrivateKey, ts time.Time,
userPubKey *rsa.PublicKey) ([]byte, error) {
// Construct the hash
options := rsa.NewDefaultOptions()
// Digest the timestamp and public key
hashedData := digestTimestamp(options.Hash.New(), ts, userPubKey)
hashedData := digest(options.Hash.New(), ts, userPubKey)
// Sign the data
return rsa.Sign(rand, priv, options.Hash, hashedData, options)
}
// VerifyTimestamp verifies the signature provided against serverPubKey and the
// VerifyWithTimestamp verifies the signature provided against serverPubKey and the
// digest of the timestamp ts and userPubKey
func VerifyTimestamp(sig []byte, serverPubKey *rsa.PublicKey,
func VerifyWithTimestamp(sig []byte, serverPubKey *rsa.PublicKey,
ts time.Time, userPubKey *rsa.PublicKey) error {
// Construct the hash
options := rsa.NewDefaultOptions()
// Digest the timestamp and public key
hashedData := digestTimestamp(options.Hash.New(), ts, userPubKey)
hashedData := digest(options.Hash.New(), ts, userPubKey)
// Verify the signature
return rsa.Verify(serverPubKey, options.Hash, hashedData, sig, options)
}
// digestTimestamp is a helper function which digests the timestamp ts and
// digest is a helper function which digests the timestamp ts and
// rsa.PublicKey userPubKey given hash h
func digestTimestamp(h hash.Hash, ts time.Time, userPubKey *rsa.PublicKey) []byte {
func digest(h hash.Hash, ts time.Time, userPubKey *rsa.PublicKey) []byte {
// Serialize the public key
pubKeyBytes := xx.PublicKeyBytes(&(*userPubKey).PublicKey)
......
......@@ -122,7 +122,7 @@ func TestSignVerify(t *testing.T) {
}
// Sign data
sig, err := SignTimestamp(notRand, serverPrivKey, testTime, userPrivKey.GetPublic())
sig, err := SignWithTimestamp(notRand, serverPrivKey, testTime, userPrivKey.GetPublic())
if err != nil {
t.Fatalf("SignVerify error: "+
"Could not sign data: %v", err.Error())
......@@ -136,7 +136,7 @@ func TestSignVerify(t *testing.T) {
}
// Test the verification
err = VerifyTimestamp(sig, serverPrivKey.GetPublic(), testTime, userPrivKey.GetPublic())
err = VerifyWithTimestamp(sig, serverPrivKey.GetPublic(), testTime, userPrivKey.GetPublic())
if err != nil {
t.Fatalf("SignVerify error: "+
"Could not verify signature: %v", err.Error())
......@@ -156,14 +156,14 @@ func TestSignVerify(t *testing.T) {
"Could not generate key: %v", err.Error())
}
sig, err = SignTimestamp(notRand, serverPrivKey, testTime, userPrivKey.GetPublic())
sig, err = SignWithTimestamp(notRand, serverPrivKey, testTime, userPrivKey.GetPublic())
if err != nil {
t.Fatalf("SignVerify error: "+
"Could not sign data: %v", err.Error())
}
// Test the verification
err = VerifyTimestamp(sig, serverPrivKey.GetPublic(), testTime, userPrivKey.GetPublic())
err = VerifyWithTimestamp(sig, serverPrivKey.GetPublic(), testTime, userPrivKey.GetPublic())
if err != nil {
t.Fatalf("SignVerify error: "+
"Could not verify signature: %v", err.Error())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment