41 lines
2.3 KiB
TypeScript
41 lines
2.3 KiB
TypeScript
import * as crypto from 'node:crypto';
|
|
import { genRSAAndEd25519KeyPair } from '@/misc/gen-key-pair.js';
|
|
|
|
describe(genRSAAndEd25519KeyPair, () => {
|
|
test('generates key pair', async () => {
|
|
const keyPair = await genRSAAndEd25519KeyPair();
|
|
// 毎回違うキーペアが生成されることを確認するために2回生成して比較してみる
|
|
const keyPair2 = await genRSAAndEd25519KeyPair();
|
|
console.log(Object.entries(keyPair).map(([k, v]) => `${k}: ${v.length}`).join('\n'));
|
|
console.log(Object.entries(keyPair).map(([k, v]) => `${k}\n${v}`).join('\n'));
|
|
|
|
expect(keyPair.publicKey).toMatch(/^-----BEGIN PUBLIC KEY-----/);
|
|
expect(keyPair.publicKey).toMatch(/-----END PUBLIC KEY-----\n$/);
|
|
expect(keyPair.publicKey).not.toBe(keyPair2.publicKey);
|
|
|
|
const publicKeyObj = crypto.createPublicKey(keyPair.publicKey);
|
|
expect(publicKeyObj.asymmetricKeyType).toBe('rsa');
|
|
|
|
expect(keyPair.privateKey).toMatch(/^-----BEGIN PRIVATE KEY-----/);
|
|
expect(keyPair.privateKey).toMatch(/-----END PRIVATE KEY-----\n$/);
|
|
expect(keyPair.privateKey).not.toBe(keyPair2.privateKey);
|
|
expect(keyPair.ed25519PublicKey).toMatch(/^-----BEGIN PUBLIC KEY-----/);
|
|
expect(keyPair.ed25519PublicKey).toMatch(/-----END PUBLIC KEY-----\n$/);
|
|
expect(keyPair.ed25519PublicKey).not.toBe(keyPair2.ed25519PublicKey);
|
|
|
|
const ed25519PublicKeyObj = crypto.createPublicKey(keyPair.ed25519PublicKey);
|
|
expect(ed25519PublicKeyObj.asymmetricKeyType).toBe('ed25519');
|
|
|
|
expect(keyPair.ed25519PrivateKey).toMatch(/^-----BEGIN PRIVATE KEY-----/);
|
|
expect(keyPair.ed25519PrivateKey).toMatch(/-----END PRIVATE KEY-----\n$/);
|
|
expect(keyPair.ed25519PrivateKey).not.toBe(keyPair2.ed25519PrivateKey);
|
|
expect(keyPair.ed25519PublicKeySignature).toBe(
|
|
crypto.sign(keyPair.ed25519SignatureAlgorithm.split('-').pop(), Buffer.from(keyPair.ed25519PublicKey), keyPair.privateKey).toString('base64'),
|
|
);
|
|
expect(crypto.verify(keyPair.ed25519SignatureAlgorithm, Buffer.from(keyPair.ed25519PublicKey), keyPair.publicKey, Buffer.from(keyPair.ed25519PublicKeySignature, 'base64'))).toBe(true);
|
|
expect(keyPair.ed25519PublicKeySignature).not.toBe(keyPair2.ed25519PublicKeySignature);
|
|
|
|
//const imported = await webCrypto.subtle.importKey('spki', Buffer.from(keyPair.publicKey).buffer, { name: 'rsa-pss', hash: 'sha-256' }, false, ['verify']);
|
|
});
|
|
});
|