cmapi/sign.go
Ho Sy Tan 9084d268f6
Some checks failed
gofmt / Run gofmt (push) Has been cancelled
Build and test / Build and test on Ubuntu (push) Has been cancelled
2025-04-03 16:23:58
2025-04-03 16:23:58 +07:00

41 lines
791 B
Go

package cmapi
import (
"encoding/base64"
"encoding/json"
"time"
"git.ct129.com/cmesh/cmapi/keys"
"git.ct129.com/cmesh/cmapi/message"
)
func SignRequestV1(reqType string, value []byte, hostID string, counter uint, privkey keys.PrivateKey) ([]byte, error) {
encMsg, err := json.Marshal(message.RequestWrapper{
Type: reqType,
Value: value,
Timestamp: time.Now(),
})
if err != nil {
return nil, err
}
signedMsg := base64.StdEncoding.EncodeToString(encMsg)
sig, err := privkey.Sign([]byte(signedMsg))
if err != nil {
return nil, err
}
wrapper := message.RequestV1{
Version: 1,
HostID: hostID,
Counter: counter,
Message: signedMsg,
Signature: sig,
}
b, err := json.Marshal(wrapper)
if err != nil {
return nil, err
}
return b, nil
}