github.com/decred/politeia@v1.4.0/util/identity.go (about)

     1  // Copyright (c) 2017-2019 The Decred developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"encoding/hex"
     9  	"fmt"
    10  
    11  	"github.com/decred/politeia/politeiad/api/v1/identity"
    12  )
    13  
    14  // VerifyChallenge checks that the signature returned from politeiad is the
    15  // challenge signed with the given identity.
    16  func VerifyChallenge(id *identity.PublicIdentity, challenge []byte, signature string) error {
    17  	// Verify challenge.
    18  	s, err := hex.DecodeString(signature)
    19  	if err != nil {
    20  		return err
    21  	}
    22  	var sig [identity.SignatureSize]byte
    23  	copy(sig[:], s)
    24  	if !id.VerifyMessage(challenge, sig) {
    25  		return fmt.Errorf("challenge verification failed")
    26  	}
    27  
    28  	return nil
    29  }