2024-02-01 16:41:39 +05:30
|
|
|
package security
|
2021-02-26 07:15:45 +05:30
|
|
|
|
|
|
|
import (
|
2024-02-13 06:38:42 +05:30
|
|
|
"context"
|
2021-02-26 07:15:45 +05:30
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/base64"
|
|
|
|
)
|
|
|
|
|
|
|
|
var randomReader = rand.Reader
|
|
|
|
|
2024-02-01 16:41:39 +05:30
|
|
|
func NewSigner(key *rsa.PrivateKey) *Signer {
|
|
|
|
return &Signer{Key: key}
|
|
|
|
}
|
|
|
|
|
2021-02-26 07:15:45 +05:30
|
|
|
type Signer struct {
|
|
|
|
Key *rsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
2024-02-13 06:38:42 +05:30
|
|
|
func (s *Signer) SignTextures(ctx context.Context, textures string) (string, error) {
|
2021-02-26 07:15:45 +05:30
|
|
|
message := []byte(textures)
|
|
|
|
messageHash := sha1.New()
|
2024-02-01 16:41:39 +05:30
|
|
|
_, err := messageHash.Write(message)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-02-26 07:15:45 +05:30
|
|
|
|
2024-02-01 16:41:39 +05:30
|
|
|
messageHashSum := messageHash.Sum(nil)
|
2021-02-26 07:15:45 +05:30
|
|
|
signature, err := rsa.SignPKCS1v15(randomReader, s.Key, crypto.SHA1, messageHashSum)
|
|
|
|
if err != nil {
|
2024-02-01 16:41:39 +05:30
|
|
|
return "", err
|
2021-02-26 07:15:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(signature), nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 06:38:42 +05:30
|
|
|
func (s *Signer) GetPublicKey(ctx context.Context) (*rsa.PublicKey, error) {
|
2021-02-26 07:15:45 +05:30
|
|
|
return &s.Key.PublicKey, nil
|
|
|
|
}
|