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

PeculiarVentures / x509 / 18688905299

21 Oct 2025 03:22PM UTC coverage: 89.87% (+0.07%) from 89.797%
18688905299

push

github

web-flow
Merge pull request #111 from PeculiarVentures/donskov/eslint-migration

Update ESLint configuration and dependencies

901 of 1050 branches covered (85.81%)

Branch coverage included in aggregate %.

438 of 483 new or added lines in 36 files covered. (90.68%)

4 existing lines in 3 files now uncovered.

3242 of 3560 relevant lines covered (91.07%)

44.01 hits per line

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

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

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

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

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

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

41
    return null;
×
42
  }
2✔
43

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

58
          switch (hash.toLowerCase()) {
44✔
59
            case "sha-1":
46✔
60
              return new AlgorithmIdentifier({
4✔
61
                algorithm: asn1Rsa.id_sha1WithRSAEncryption, parameters: null,
4✔
62
              });
4✔
63
            case "sha-256":
46✔
64
              return new AlgorithmIdentifier({
4✔
65
                algorithm: asn1Rsa.id_sha256WithRSAEncryption, parameters: null,
4✔
66
              });
4✔
67
            case "sha-384":
46✔
68
              return new AlgorithmIdentifier({
34✔
69
                algorithm: asn1Rsa.id_sha384WithRSAEncryption, parameters: null,
34✔
70
              });
34✔
71
            case "sha-512":
46!
NEW
72
              return new AlgorithmIdentifier({
×
NEW
73
                algorithm: asn1Rsa.id_sha512WithRSAEncryption, parameters: null,
×
NEW
74
              });
×
75
          }
46✔
76
        } else {
48✔
77
          return new AlgorithmIdentifier({
2✔
78
            algorithm: asn1Rsa.id_rsaEncryption, parameters: null,
2✔
79
          });
2✔
80
        }
2✔
81
        break;
2✔
82
      case "rsa-pss":
54✔
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),
2✔
94
          });
2✔
95
        } else {
2!
NEW
96
          return new AlgorithmIdentifier({
×
NEW
97
            algorithm: asn1Rsa.id_RSASSA_PSS, parameters: null,
×
NEW
98
          });
×
99
        }
×
100
        break;
×
101
    }
54✔
102

103
    return null;
6✔
104
  }
54✔
105

106
  public toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | HashedAlgorithm | null {
2✔
107
    switch (alg.algorithm) {
164✔
108
      case asn1Rsa.id_rsaEncryption:
164✔
109
        return { name: "RSASSA-PKCS1-v1_5" };
102✔
110
      case asn1Rsa.id_sha1WithRSAEncryption:
164!
NEW
111
        return {
×
NEW
112
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-1" },
×
NEW
113
        };
×
114
      case asn1Rsa.id_sha256WithRSAEncryption:
164✔
115
        return {
14✔
116
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" },
14✔
117
        };
14✔
118
      case asn1Rsa.id_sha384WithRSAEncryption:
164✔
119
        return {
28✔
120
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-384" },
28✔
121
        };
28✔
122
      case asn1Rsa.id_sha512WithRSAEncryption:
164!
NEW
123
        return {
×
NEW
124
          name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-512" },
×
NEW
125
        };
×
126
      case asn1Rsa.id_RSASSA_PSS:
164✔
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",
4✔
134
            hash: hashAlg,
4✔
135
            saltLength: pssParams.saltLength,
4✔
136
          } as Algorithm;
4✔
137
        } else {
4!
138
          return { name: "RSA-PSS" };
×
139
        }
×
140
    }
164✔
141

142
    return null;
16✔
143
  }
164✔
144
}
2✔
145

146
// register RSA algorithm provider as a singleton object
147
container.registerSingleton(diAlgorithm, RsaAlgorithm);
2✔
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