mirror of
https://github.com/elyby/chrly.git
synced 2024-11-30 02:32:19 +05:30
6f148a8791
Implemented /signing-key endpoint to get public key in der format, used to sign the textures. Improved logging of errors from http package. Changed behavior of the /cloaks endpoint
43 lines
783 B
Go
43 lines
783 B
Go
package signer
|
|
|
|
import (
|
|
"crypto"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"errors"
|
|
)
|
|
|
|
var randomReader = rand.Reader
|
|
|
|
type Signer struct {
|
|
Key *rsa.PrivateKey
|
|
}
|
|
|
|
func (s *Signer) SignTextures(textures string) (string, error) {
|
|
if s.Key == nil {
|
|
return "", errors.New("Key is empty")
|
|
}
|
|
|
|
message := []byte(textures)
|
|
messageHash := sha1.New()
|
|
_, _ = messageHash.Write(message)
|
|
messageHashSum := messageHash.Sum(nil)
|
|
|
|
signature, err := rsa.SignPKCS1v15(randomReader, s.Key, crypto.SHA1, messageHashSum)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return base64.StdEncoding.EncodeToString(signature), nil
|
|
}
|
|
|
|
func (s *Signer) GetPublicKey() (*rsa.PublicKey, error) {
|
|
if s.Key == nil {
|
|
return nil, errors.New("Key is empty")
|
|
}
|
|
|
|
return &s.Key.PublicKey, nil
|
|
}
|