Beispiel #1
0
// Signs a block. The signing process takes the value of a block and signs
// it using JWS. The signature generated is included in the
// `signatures` block. If a block is empty or unknown, an error is returned.
func (self *Stone) Sign(blockName string, privateKey string) (string, error) {

	var block map[string]interface{}

	signer, err := crypto.ParsePrivateKey([]byte(privateKey))
	if err != nil {
		return "", errors.New("Private Key Error: " + err.Error())
	}

	// block name must be known
	if !util.InStringSlice(KnownBlockNames, blockName) {
		return "", errors.New("block unknown")
	}

	block = self.getBlock(blockName)
	if util.IsMapEmpty(block) {
		return "", errors.New("failed to sign empty block")
	}

	// sign block
	payload, _ := util.MapToJSON(block)
	signature, err := signer.JWS_RSA_Sign(payload)
	if err != nil {
		return "", errors.New("failed to sign block")
	}

	self.Signatures[blockName] = signature
	return signature, nil
}
Beispiel #2
0
// TestEncodeSuccessfully tests that a stone was encoded successfully
func TestEncodeSuccessfully(t *testing.T) {
	var meta = map[string]interface{}{
		"id":         util.NewID(),
		"type":       "currency",
		"created_at": time.Now().Unix(),
	}
	sh, err := Create(meta, util.ReadFromFixtures("tests/fixtures/rsa_priv_1.txt"))
	assert.Nil(t, err)
	enc, _ := util.MapToJSON(sh.Signatures)
	expectedEncodeVal := crypto.ToBase64Raw([]byte(enc))
	assert.Equal(t, sh.Encode(), expectedEncodeVal)
}
Beispiel #3
0
// Returns a base64url encoded string of the signatures block
func (self *Stone) Encode() string {
	var signaturesStr, _ = util.MapToJSON(self.Signatures)
	return crypto.ToBase64Raw([]byte(signaturesStr))
}