UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

83 lines 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const CryptoFactory_1 = require("../../src/crypto/plugin/CryptoFactory"); const src_1 = require("../../src"); const webcrypto_core_1 = require("webcrypto-core"); const KeyTypeFactory_1 = require("../../src/crypto/keys/KeyTypeFactory"); const KeyUseFactory_1 = require("../../src/crypto/keys/KeyUseFactory"); const JsonWebKey_1 = require("../../src/crypto/keys/JsonWebKey"); /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ class TestSubtle extends webcrypto_core_1.SubtleCrypto { constructor(alg) { super(); this.alg = alg; } getSign(name) { return this.alg + name; } } describe('CryptoHelpers', () => { it('should getSubtleCryptoForAlgorithm', () => { const keyStore = new src_1.KeyStoreInMemory(); const crypto = new TestSubtle('name-'); const cryptoFactory = new CryptoFactory_1.default(keyStore, crypto); let subtle = src_1.CryptoHelpers.getSubtleCryptoForAlgorithm(cryptoFactory, { name: 'SHA-512' }); expect(subtle.getSign('test')).toEqual('name-test'); subtle = src_1.CryptoHelpers.getSubtleCryptoForAlgorithm(cryptoFactory, { name: 'HMAC' }); expect(subtle.getSign('test')).toEqual('name-test'); let throwed = false; try { src_1.CryptoHelpers.getSubtleCryptoForAlgorithm(cryptoFactory, { name: 'SHA' }); } catch (err) { throwed = true; expect(err.message).toEqual(`Algorithm '{"name":"SHA"}' is not supported`); } expect(throwed).toBeTruthy(); }); it('should getKeyImportAlgorithm', () => { const keyStore = new src_1.KeyStoreInMemory(); const crypto = new TestSubtle('name-'); const cryptoFactory = new CryptoFactory_1.default(keyStore, crypto); const publicKey = { kty: KeyTypeFactory_1.KeyType.RSA, use: KeyUseFactory_1.KeyUse.Signature, alg: 'RS256', kid: '#key1', key_ops: [JsonWebKey_1.KeyOperation.Verify] }; const key = new src_1.RsaPublicKey(publicKey); const algorithm = src_1.CryptoHelpers.getKeyImportAlgorithm({ name: 'SHA-512' }, key); expect(algorithm.name).toEqual('SHA-512'); let throwed = false; try { src_1.CryptoHelpers.getKeyImportAlgorithm({ name: 'SHA-888' }, key); } catch (err) { throwed = true; expect(err.message).toEqual(`Algorithm '{"name":"SHA-888"}' is not supported`); } expect(throwed).toBeTruthy(); }); it('should jwaToWebCrypto', () => { const algorithm = src_1.CryptoHelpers.jwaToWebCrypto('A192GCM'); expect(algorithm.name).toEqual('AES-GCM'); let throwed = false; try { src_1.CryptoHelpers.jwaToWebCrypto('SHA-888'); } catch (err) { throwed = true; expect(err.message).toEqual(`Algorithm "SHA-888" is not supported`); } expect(throwed).toBeTruthy(); }); it('should webCryptoToJwa', () => { const algorithm = src_1.CryptoHelpers.webCryptoToJwa({ name: 'RSA-OAEP-256' }); expect(algorithm).toEqual('RSA-OAEP-256'); }); }); //# sourceMappingURL=CryptoHelpers.spec.js.map