Skip to content

Commit 3186886

Browse files
committed
Use constants for algorithms
1 parent c483eed commit 3186886

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExampleSign() {
3939
component.New("date"),
4040
).
4141
KeyID("my-key-id").
42-
Algorithm("rsa-pss-sha512").
42+
Algorithm(htmsig.AlgorithmRSAPSSSHA512).
4343
Build()
4444
if err != nil {
4545
panic(err)
@@ -89,7 +89,7 @@ func ExampleVerify() {
8989
component.New("date"),
9090
).
9191
KeyID("my-key-id").
92-
Algorithm("rsa-pss-sha512").
92+
Algorithm(htmsig.AlgorithmRSAPSSSHA512).
9393
Build()
9494
if err != nil {
9595
panic(err)

htmsig.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const (
2020
SignatureHeader = "Signature"
2121
)
2222

23+
// RFC 9421 Algorithm Names (Section 6.2.2 Initial Contents)
24+
const (
25+
AlgorithmRSAPSSSHA512 = "rsa-pss-sha512" // Section 3.3.1
26+
AlgorithmRSAV15SHA256 = "rsa-v1_5-sha256" // Section 3.3.2
27+
AlgorithmHMACSHA256 = "hmac-sha256" // Section 3.3.3
28+
AlgorithmECDSAP256SHA256 = "ecdsa-p256-sha256" // Section 3.3.4
29+
AlgorithmECDSAP384SHA384 = "ecdsa-p384-sha384" // Section 3.3.5
30+
AlgorithmEd25519 = "ed25519" // Section 3.3.6
31+
)
32+
2333
// KeyResolver interface allows resolving cryptographic keys by their ID
2434
type KeyResolver interface {
2535
ResolveKey(keyID string) (any, error)
@@ -233,17 +243,17 @@ func determineAlgorithm(def *input.Definition, key any) (string, error) {
233243
func convertRFC9421ToDSIG(rfc9421Alg string) (string, error) {
234244
switch rfc9421Alg {
235245
// Official RFC 9421 algorithms from Section 6.2.2 Initial Contents
236-
case "rsa-pss-sha512": // Section 3.3.1
246+
case AlgorithmRSAPSSSHA512: // Section 3.3.1
237247
return dsig.RSAPSSWithSHA512, nil
238-
case "rsa-v1_5-sha256": // Section 3.3.2
248+
case AlgorithmRSAV15SHA256: // Section 3.3.2
239249
return dsig.RSAPKCS1v15WithSHA256, nil
240-
case "hmac-sha256": // Section 3.3.3
250+
case AlgorithmHMACSHA256: // Section 3.3.3
241251
return dsig.HMACWithSHA256, nil
242-
case "ecdsa-p256-sha256": // Section 3.3.4
252+
case AlgorithmECDSAP256SHA256: // Section 3.3.4
243253
return dsig.ECDSAWithP256AndSHA256, nil
244-
case "ecdsa-p384-sha384": // Section 3.3.5
254+
case AlgorithmECDSAP384SHA384: // Section 3.3.5
245255
return dsig.ECDSAWithP384AndSHA384, nil
246-
case "ed25519": // Section 3.3.6
256+
case AlgorithmEd25519: // Section 3.3.6
247257
return dsig.EdDSA, nil
248258
default:
249259
return "", fmt.Errorf("unsupported RFC 9421 algorithm: %s", rfc9421Alg)

rfc9421_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,31 +560,31 @@ func TestRFC9421_MultipleSignatures(t *testing.T) {
560560
Components(component.Method(), component.Authority()).
561561
Created(created).
562562
KeyID("test-key-rsa-pss").
563-
Algorithm("rsa-pss-sha512").
563+
Algorithm(htmsig.AlgorithmRSAPSSSHA512).
564564
MustBuild()
565565

566566
ecdsaDef := input.NewDefinitionBuilder().
567567
Label("sig-ecdsa").
568568
Components(component.New("date"), component.New("content-type")).
569569
Created(created).
570570
KeyID("test-key-ecc-p256").
571-
Algorithm("ecdsa-p256-sha256").
571+
Algorithm(htmsig.AlgorithmECDSAP256SHA256).
572572
MustBuild()
573573

574574
ed25519Def := input.NewDefinitionBuilder().
575575
Label("sig-ed25519").
576576
Components(component.Authority(), component.New("content-length")).
577577
Created(created).
578578
KeyID("test-key-ed25519").
579-
Algorithm("ed25519").
579+
Algorithm(htmsig.AlgorithmEd25519).
580580
MustBuild()
581581

582582
hmacDef := input.NewDefinitionBuilder().
583583
Label("sig-hmac").
584584
Components(component.New("date"), component.Method()).
585585
Created(created).
586586
KeyID("test-shared-secret").
587-
Algorithm("hmac-sha256").
587+
Algorithm(htmsig.AlgorithmHMACSHA256).
588588
MustBuild()
589589

590590
ctx := component.WithRequestInfoFromHTTP(context.Background(), req)

0 commit comments

Comments
 (0)