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

[v7] Fix invalid default creation time in generateKey #137

Merged
merged 1 commit into from
Feb 15, 2022
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: 1 addition & 1 deletion lib/key/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import encryptMessage from '../message/encrypt';
* @returns {Promise<Object>} generated key data and armored revocation certificate in the form:
* { PrivateKey, PublicKey : String|Uint8Array|Object, revocationCertificate: String }
*/
export async function generateKey({ date = new Date(serverTime() + DEFAULT_OFFSET), ...rest }) {
export async function generateKey({ date = new Date(+serverTime() + DEFAULT_OFFSET), ...rest }) {
return openpgp_generateKey({ date, ...rest });
}

Expand Down
12 changes: 11 additions & 1 deletion test/key/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { enums, generateKey, PrivateKey, readKey, revokeKey, sign, createMessage } from '../../lib/openpgp';
import { enums, PrivateKey, readKey, revokeKey, sign, createMessage } from '../../lib/openpgp';
import {
concatArrays,
decodeBase64,
Expand All @@ -8,6 +8,7 @@ import {
isRevokedKey,
reformatKey,
getKey,
generateKey,
getMatchingKey,
generateSessionKey,
generateSessionKeyFromKeyPreferences,
Expand Down Expand Up @@ -76,6 +77,15 @@ GAlY9rxVStLBrg0Hn+5gkhyHI9B85rM1BEYXQ8pP5CSFuTwbJ3O2s67dzQ==
).to.throw(/UserID does not contain correct email address/);
});

it('generateKey - it has valid default creation time', async () => {
const { privateKey } = await generateKey({
userIDs: [{ name: 'name', email: 'email@test.com' }],
format: 'object'
});
const now = new Date();
expect(Math.abs(+privateKey.getCreationTime() - +now) < 24 * 3600).to.be.true;
});

it('reformatKey - it reformats a key using the key creation time', async () => {
const date = new Date(0);
const { privateKey } = await generateKey({
Expand Down