• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeculiarVentures / x509 / 23501539988

24 Mar 2026 04:49PM UTC coverage: 88.195% (+2.8%) from 85.411%
23501539988

Pull #123

github

web-flow
Merge 3e02dbf3c into e6979dfda
Pull Request #123: Improve type safety and remove use of any

770 of 943 branches covered (81.65%)

Branch coverage included in aggregate %.

65 of 70 new or added lines in 8 files covered. (92.86%)

1 existing line in 1 file now uncovered.

1561 of 1700 relevant lines covered (91.82%)

53.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.44
/src/rsa_algorithm.ts
1
import * as asn1Rsa from "@peculiar/asn1-rsa";
2
import { AsnConvert } from "@peculiar/asn1-schema";
3
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
4
import { container, injectable } from "tsyringe";
5
import {
6
  AlgorithmProvider, diAlgorithm, diAlgorithmProvider, IAlgorithm,
7
} from "./algorithm";
8
import { HashedAlgorithm } from "./types";
9

10
/**
11
 * RSA algorithm provider
12
 */
13
@injectable()
14
export class RsaAlgorithm implements IAlgorithm {
32✔
15
  public static createPssParams(hash: unknown, saltLength: number): asn1Rsa.RsaSaPssParams | null {
16
    const hashAlgorithm = RsaAlgorithm.getHashAlgorithm(hash);
2✔
17
    if (!hashAlgorithm) {
2!
18
      return null;
×
19
    }
20

21
    return new asn1Rsa.RsaSaPssParams({
2✔
22
      hashAlgorithm,
23
      maskGenAlgorithm: new AlgorithmIdentifier({
24
        algorithm: asn1Rsa.id_mgf1,
25
        parameters: AsnConvert.serialize(hashAlgorithm),
26
      }),
27
      saltLength,
28
    });
29
  }
30

31
  public static getHashAlgorithm(alg: unknown): AlgorithmIdentifier | null {
32
    const algProv = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
2✔
33
    if (typeof alg === "string") {
2!
34
      return algProv.toAsnAlgorithm({ name: alg });
×
35
    }
36

37
    if (typeof alg === "object" && alg && "name" in alg) {
2!
38
      return algProv.toAsnAlgorithm(alg as Algorithm);
2✔
39
    }
40

41
    return null;
×
42
  }
43

44
  public toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null {
45
    switch (alg.name.toLowerCase()) {
62✔
46
      case "rsassa-pkcs1-v1_5":
47
        if ("hash" in alg) {
56✔
48
          let hash: string;
49
          if (typeof alg.hash === "string") {
54!
UNCOV
50
            hash = alg.hash;
×
51
          } else if (alg.hash && typeof alg.hash === "object"
54✔
52
            && "name" in alg.hash && typeof alg.hash.name === "string") {
53
            hash = alg.hash.name.toUpperCase();
52✔
54
          } else {
55
            throw new Error("Cannot get hash algorithm name");
2✔
56
          }
57

58
          switch (hash.toLowerCase()) {
52!
59
            case "sha-1":
60
              return new AlgorithmIdentifier({
4✔
61
                algorithm: asn1Rsa.id_sha1WithRSAEncryption, parameters: null,
62
              });
63
            case "sha-256":
64
              return new AlgorithmIdentifier({
12✔
65
                algorithm: asn1Rsa.id_sha256WithRSAEncryption, parameters: null,
66
              });
67
            case "sha-384":
68
              return new AlgorithmIdentifier({
34✔
69
                algorithm: asn1Rsa.id_sha384WithRSAEncryption, parameters: null,
70
              });
71
            case "sha-512":
72
              return new AlgorithmIdentifier({
×
73
                algorithm: asn1Rsa.id_sha512WithRSAEncryption, parameters: null,
74
              });
75
          }
76
        } else {
77
          return new AlgorithmIdentifier({
2✔
78
            algorithm: asn1Rsa.id_rsaEncryption, parameters: null,
79
          });
80
        }
81
        break;
2✔
82
      case "rsa-pss":
83
        if ("hash" in alg) {
2!
84
          if (!("saltLength" in alg && typeof alg.saltLength === "number")) {
2!
85
            throw new Error("Cannot get 'saltLength' from 'alg' argument");
×
86
          }
87
          const pssParams = RsaAlgorithm.createPssParams(alg.hash, alg.saltLength);
2✔
88
          if (!pssParams) {
2!
89
            throw new Error("Cannot create PSS parameters");
×
90
          }
91

92
          return new AlgorithmIdentifier({
2✔
93
            algorithm: asn1Rsa.id_RSASSA_PSS, parameters: AsnConvert.serialize(pssParams),
94
          });
95
        } else {
96
          return new AlgorithmIdentifier({
×
97
            algorithm: asn1Rsa.id_RSASSA_PSS, parameters: null,
98
          });
99
        }
100
        break;
×
101
    }
102

103
    return null;
6✔
104
  }
105

106
  public toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | HashedAlgorithm | null {
107
    switch (alg.algorithm) {
166!
108
      case asn1Rsa.id_rsaEncryption:
109
        return { name: "RSASSA-PKCS1-v1_5" };
102✔
110
      case asn1Rsa.id_sha1WithRSAEncryption:
111
        return {
×
112
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-1" },
113
        };
114
      case asn1Rsa.id_sha256WithRSAEncryption:
115
        return {
16✔
116
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" },
117
        };
118
      case asn1Rsa.id_sha384WithRSAEncryption:
119
        return {
28✔
120
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-384" },
121
        };
122
      case asn1Rsa.id_sha512WithRSAEncryption:
123
        return {
×
124
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-512" },
125
        };
126
      case asn1Rsa.id_RSASSA_PSS:
127
        if (alg.parameters) {
4!
128
          const pssParams = AsnConvert.parse(alg.parameters, asn1Rsa.RsaSaPssParams);
4✔
129
          const algProv = container.resolve<AlgorithmProvider>(diAlgorithmProvider);
4✔
130
          const hashAlg = algProv.toWebAlgorithm(pssParams.hashAlgorithm);
4✔
131

132
          return {
4✔
133
            name: "RSA-PSS",
134
            hash: hashAlg,
135
            saltLength: pssParams.saltLength,
136
          } as Algorithm;
137
        } else {
138
          return { name: "RSA-PSS" };
×
139
        }
140
    }
141

142
    return null;
16✔
143
  }
144
}
145

146
// register RSA algorithm provider as a singleton object
147
container.registerSingleton(diAlgorithm, RsaAlgorithm);
32✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc