Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getSHA256Fingerprints: performance improvement for v6 keys (reuse key fingerprints) #207

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/key/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ export interface GenerateSessionKeyOptionsPmcrypto extends Omit<GenerateSessionK
}
export function generateSessionKey(options: GenerateSessionKeyOptionsPmcrypto): Promise<SessionKey>;

export function getFingerprint(key: Key): string;

export function isExpiredKey(key: Key, date?: Date): Promise<boolean>;
export function isRevokedKey(key: Key, date?: Date): Promise<boolean>;

Expand Down
12 changes: 6 additions & 6 deletions lib/key/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export const canKeyEncrypt = async (publicKey, date = serverTime()) => {
}
};

export function getFingerprint(key) {
return key.getFingerprint();
}

export const getSHA256Fingerprints = (key) => {
return Promise.all(
key.getKeys().map(async ({ keyPacket }) => {
return arrayToHexString(await SHA256(keyPacket.writeForHash(keyPacket.version)));
key.getKeys().map(async (keyOrSubkey) => {
const { version } = keyOrSubkey.keyPacket;
const keyFingerprintIsSHA256 = version === 5 || version === 6;
return keyFingerprintIsSHA256 ?
keyOrSubkey.getFingerprint() :
arrayToHexString(await SHA256(keyOrSubkey.keyPacket.writeForHash(version)));
})
);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/pmcrypto.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export {
generateKey, reformatKey,
generateSessionKey, generateSessionKeyForAlgorithm,
isExpiredKey, isRevokedKey, canKeyEncrypt,
getFingerprint, getSHA256Fingerprints,
getSHA256Fingerprints,
getMatchingKey
} from './key/utils';

Expand Down
1 change: 0 additions & 1 deletion lib/pmcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export {
isExpiredKey,
isRevokedKey,
canKeyEncrypt,
getFingerprint,
getMatchingKey,
getSHA256Fingerprints
} from './key/utils';
Expand Down
136 changes: 136 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/chai-as-promised": "^7.1.8",
"@types/elliptic": "^6.4.18",
"@types/mocha": "^9.1.1",
"@types/sinon": "^17.0.3",
"@types/webpack-env": "^1.18.5",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
Expand All @@ -62,6 +63,7 @@
"karma-webpack": "^5.0.1",
"mocha": "^11.0.1",
"playwright": "^1.48.2",
"sinon": "^19.0.2",
"ts-loader": "^9.5.1",
"typescript": "^5.6.3",
"web-streams-polyfill": "^3.3.3",
Expand Down
19 changes: 14 additions & 5 deletions test/key/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
import { createSandbox as createSinonSandbox } from 'sinon';
import { revokeKey, sign, createMessage, enums } from '../../lib/openpgp';
import {
isExpiredKey,
Expand Down Expand Up @@ -54,11 +55,19 @@ T/efFOC6BDkAAHcjAPwIPNHnR9bKmkVop6cE05dCIpZ/W8zXDGnjKYrrC4Hb
const { publicKey } = await generateKey({ userIDs: [{}], passphrase: 'test', config: { v6Keys: true }, format: 'object' });

const fingerprints = publicKey.getKeys().map((key) => key.getFingerprint());
const sha256Fingerprints = await getSHA256Fingerprints(publicKey);
expect(sha256Fingerprints.length).to.equal(fingerprints.length);
sha256Fingerprints.forEach((sha256Fingerprint, i) => {
expect(sha256Fingerprint).to.equal(fingerprints[i]);
});
const sinonSandbox = createSinonSandbox();
try {
const webcryptoHashSpy = sinonSandbox.spy(crypto.subtle, 'digest');
const sha256Fingerprints = await getSHA256Fingerprints(publicKey);
// ensure no hashing is done for v6 keys; the fingerprints stored in the key are expected to be returned.
expect(webcryptoHashSpy.called).to.be.false;
expect(sha256Fingerprints.length).to.equal(fingerprints.length);
sha256Fingerprints.forEach((sha256Fingerprint, i) => {
expect(sha256Fingerprint).to.equal(fingerprints[i]);
});
} finally {
sinonSandbox.restore();
}
});

it('generateKey - it has valid default creation time', async () => {
Expand Down
Loading