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

PeculiarVentures / x509 / 15564777427

10 Jun 2025 04:16PM UTC coverage: 90.282% (+1.9%) from 88.355%
15564777427

push

github

microshine
chore(deps): deduplicate dependencies

726 of 843 branches covered (86.12%)

Branch coverage included in aggregate %.

2795 of 3057 relevant lines covered (91.43%)

55.14 hits per line

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

83.33
/src/algorithm.ts
1
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
2✔
2
import { container } from "tsyringe";
2✔
3

4
export interface UnknownAlgorithm extends Algorithm {
5
  name: string;
6
  parameters?: ArrayBuffer | null;
7
}
8

9
export interface IAlgorithm {
10

11
  /**
12
   * Converts WebCrypto algorithm to ASN.1 algorithm
13
   * @param alg WebCrypto algorithm
14
   * @returns ASN.1 algorithm or null
15
   */
16
  toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier | null;
17

18
  /**
19
   * Converts ASN.1 algorithm to WebCrypto algorithm
20
   * @param alg ASN.1 algorithm
21
   * @returns WebCrypto algorithm or null
22
   */
23
  toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm | null;
24

25
}
26

27
/**
28
 * Dependency Injection algorithm identifier
29
 */
30
export const diAlgorithm = "crypto.algorithm";
2✔
31

32
export class AlgorithmProvider {
2✔
33

34
  /**
35
   * Returns all registered algorithm providers
36
   */
37
  public getAlgorithms() {
2✔
38
    return container.resolveAll<IAlgorithm>(diAlgorithm);
602✔
39
  }
602✔
40

41
  /**
42
   * Converts WebCrypto algorithm to ASN.1 algorithm
43
   * @param alg WebCrypto algorithm
44
   * @returns ASN.1 algorithm
45
   * @throws Error whenever cannot convert an algorithm
46
   */
47
  public toAsnAlgorithm(alg: Algorithm): AlgorithmIdentifier {
2✔
48
    // prepare hashed algorithm
49
    const algCopy: any = { ...alg };
108✔
50
    if (algCopy.hash && typeof algCopy.hash === "string") {
108✔
51
      algCopy.hash = { name: algCopy.hash };
36✔
52
    }
36✔
53

54
    for (const algorithm of this.getAlgorithms()) {
108✔
55
      const res = algorithm.toAsnAlgorithm(alg);
172✔
56
      if (res) {
172✔
57
        return res;
104✔
58
      }
104✔
59
    }
172✔
60

61
    if (/^[0-9.]+$/.test(alg.name)) {
12!
62
      const res = new AlgorithmIdentifier({
×
63
        algorithm: alg.name,
×
64
      });
×
65

66
      if ("parameters" in alg) {
×
67
        const unknown = alg as UnknownAlgorithm;
×
68
        res.parameters = unknown.parameters;
×
69
      }
×
70

71
      return res;
×
72
    }
✔
73

74
    throw new Error("Cannot convert WebCrypto algorithm to ASN.1 algorithm");
2✔
75
  }
108✔
76

77
  /**
78
   * ConvertsASN.1 algorithm to WebCrypto algorithm
79
   * @param alg ASN.1 algorithm
80
   * @returns  algorithm
81
   */
82
  public toWebAlgorithm(alg: AlgorithmIdentifier): Algorithm {
2✔
83
    for (const algorithm of this.getAlgorithms()) {
494✔
84
      const res = algorithm.toWebAlgorithm(alg);
800✔
85
      if (res) {
800✔
86
        return res;
490✔
87
      }
490✔
88
    }
800✔
89

90
    const unknown: UnknownAlgorithm = {
4✔
91
      name: alg.algorithm,
4✔
92
      parameters: alg.parameters,
4✔
93
    };
4✔
94

95
    return unknown;
4✔
96
  }
494✔
97

98
}
2✔
99

100
export const diAlgorithmProvider = "crypto.algorithmProvider";
2✔
101

102
// register AlgorithmProvider as a singleton object
103
container.registerSingleton(diAlgorithmProvider, AlgorithmProvider);
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc