xmldsigjs
Version:
XML Digital Signature implementation in TypeScript/JavaScript using Web Crypto API
83 lines (82 loc) • 2.37 kB
JavaScript
import { SignatureAlgorithm } from '../algorithm.js';
import { SHA1, SHA256, SHA384, SHA512 } from './rsa_hash.js';
export const HMAC = 'HMAC';
export const HMAC_SHA1_NAMESPACE = 'http://www.w3.org/2000/09/xmldsig#hmac-sha1';
export const HMAC_SHA256_NAMESPACE = 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha256';
export const HMAC_SHA384_NAMESPACE = 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha384';
export const HMAC_SHA512_NAMESPACE = 'http://www.w3.org/2001/04/xmldsig-more#hmac-sha512';
function fromAlgorithm(alg) {
if (alg.name.toUpperCase() === HMAC.toUpperCase()) {
switch (alg.hash.name.toUpperCase()) {
case SHA1:
return new HmacSha1();
case SHA256:
return new HmacSha256();
case SHA384:
return new HmacSha384();
case SHA512:
return new HmacSha512();
}
}
return null;
}
export class HmacSha1 extends SignatureAlgorithm {
constructor() {
super(...arguments);
this.algorithm = {
name: HMAC,
hash: {
name: SHA1,
},
};
this.namespaceURI = HMAC_SHA1_NAMESPACE;
}
static fromAlgorithm(alg) {
return fromAlgorithm(alg);
}
}
export class HmacSha256 extends SignatureAlgorithm {
constructor() {
super(...arguments);
this.algorithm = {
name: HMAC,
hash: {
name: SHA256,
},
};
this.namespaceURI = HMAC_SHA256_NAMESPACE;
}
static fromAlgorithm(alg) {
return fromAlgorithm(alg);
}
}
export class HmacSha384 extends SignatureAlgorithm {
constructor() {
super(...arguments);
this.algorithm = {
name: HMAC,
hash: {
name: SHA384,
},
};
this.namespaceURI = HMAC_SHA384_NAMESPACE;
}
static fromAlgorithm(alg) {
return fromAlgorithm(alg);
}
}
export class HmacSha512 extends SignatureAlgorithm {
constructor() {
super(...arguments);
this.algorithm = {
name: HMAC,
hash: {
name: SHA512,
},
};
this.namespaceURI = HMAC_SHA512_NAMESPACE;
}
static fromAlgorithm(alg) {
return fromAlgorithm(alg);
}
}