creating new string key Init

This commit is contained in:
Dan Ballard 2017-02-03 19:18:43 -08:00
parent a6ef43c637
commit 08433db912
1 changed files with 9 additions and 4 deletions

View File

@ -22,21 +22,26 @@ type StandardRicochetService struct {
// Init initializes a StandardRicochetService with the cryptographic key given
// by filename.
func (srs *StandardRicochetService) Init(filename string) error {
srs.ricochet = new(Ricochet)
srs.ricochet.Init()
func (srs *StandardRicochetService) InitFromKeyFile(filename string) error {
pemData, err := ioutil.ReadFile(filename)
if err != nil {
return errors.New("Could not setup ricochet service: could not read private key")
}
return srs.InitFromKey(pemData)
}
// Init initializes a StandardRicochetService with the cryptographic key
func (srs *StandardRicochetService) InitFromKey(pemData []byte) error {
srs.ricochet = new(Ricochet)
srs.ricochet.Init()
block, _ := pem.Decode(pemData)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return errors.New("Could not setup ricochet service: no valid PEM data found")
}
var err error
srs.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return errors.New("Could not setup ricochet service: could not parse private key")