Example #1
0
// Sign signs a data of a namespace
func (kml *KeyManagerLocal) Sign(proto string, namespace string, data []byte) ([]byte, error) {
	keyDir, err := kml.getKeyDir(proto, namespace)
	if err != nil {
		return nil, err
	}

	privBytes, _ := ioutil.ReadFile(filepath.Join(keyDir, defaultPrivateKey))
	return utils.SHA256Sign(privBytes, data)
}
// TestSHA256Sign
func TestSHA256Sign(t *testing.T) {
	_, path, _, _ := runtime.Caller(0)
	dir := filepath.Join(filepath.Dir(path), "testdata")

	testPrivFile := filepath.Join(dir, "rsa_private_key.pem")
	testContentFile := filepath.Join(dir, "hello.txt")
	testSignFile := filepath.Join(dir, "hello.sig")

	privBytes, _ := ioutil.ReadFile(testPrivFile)
	signBytes, _ := ioutil.ReadFile(testSignFile)
	contentBytes, _ := ioutil.ReadFile(testContentFile)
	testBytes, err := utils.SHA256Sign(privBytes, contentBytes)
	assert.Nil(t, err, "Fail to sign")
	assert.Equal(t, testBytes, signBytes, "Fail to get valid sign data ")
}