Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/public-api-server/pkg/auth/signature.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package auth
6
7
import (
8
"crypto/hmac"
9
"crypto/sha256"
10
"fmt"
11
)
12
13
type Signer interface {
14
Sign(message []byte) ([]byte, error)
15
}
16
17
func NewHS256Signer(key []byte) *HS256Signer {
18
return &HS256Signer{
19
key: key,
20
}
21
}
22
23
type HS256Signer struct {
24
key []byte
25
}
26
27
// Signs message with HS256 (HMAC with SHA-256)
28
func (s *HS256Signer) Sign(message []byte) ([]byte, error) {
29
h := hmac.New(sha256.New, s.key)
30
_, err := h.Write(message)
31
if err != nil {
32
return nil, fmt.Errorf("failed to sign secret: %w", err)
33
}
34
return h.Sum(nil), nil
35
}
36
37