src/app/_pgp/pgp-signer.ts
Properties |
Methods |
|
constructor(keyStore: MutableKeyStore)
|
||||||
Defined in src/app/_pgp/pgp-signer.ts:34
|
||||||
Parameters :
|
algo |
Type : string
|
Default value : 'sha256'
|
Defined in src/app/_pgp/pgp-signer.ts:28
|
dgst |
Type : string
|
Defined in src/app/_pgp/pgp-signer.ts:29
|
engine |
Type : string
|
Default value : 'pgp'
|
Defined in src/app/_pgp/pgp-signer.ts:27
|
keyStore |
Type : MutableKeyStore
|
Defined in src/app/_pgp/pgp-signer.ts:31
|
loggingService |
Type : LoggingService
|
Defined in src/app/_pgp/pgp-signer.ts:34
|
onsign |
Type : function
|
Defined in src/app/_pgp/pgp-signer.ts:32
|
onverify |
Type : function
|
Defined in src/app/_pgp/pgp-signer.ts:33
|
signature |
Type : Signature
|
Defined in src/app/_pgp/pgp-signer.ts:30
|
Public fingerprint |
fingerprint()
|
Defined in src/app/_pgp/pgp-signer.ts:42
|
Returns :
string
|
Public prepare | ||||||
prepare(material: Signable)
|
||||||
Defined in src/app/_pgp/pgp-signer.ts:46
|
||||||
Parameters :
Returns :
boolean
|
Public Async sign | ||||||
sign(digest: string)
|
||||||
Defined in src/app/_pgp/pgp-signer.ts:83
|
||||||
Parameters :
Returns :
Promise<void>
|
Public verify |
verify(digest: string, signature: Signature)
|
Defined in src/app/_pgp/pgp-signer.ts:51
|
Returns :
void
|
import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
import { LoggingService } from '@app/_services/logging.service';
const openpgp = require('openpgp');
interface Signable {
digest(): string;
}
interface Signature {
engine: string;
algo: string;
data: string;
digest: string;
}
interface Signer {
onsign(signature: Signature): void;
onverify(flag: boolean): void;
fingerprint(): string;
prepare(material: Signable): boolean;
verify(digest: string, signature: Signature): void;
sign(digest: string): Promise<void>;
}
class PGPSigner implements Signer {
engine = 'pgp';
algo = 'sha256';
dgst: string;
signature: Signature;
keyStore: MutableKeyStore;
onsign: (signature: Signature) => void;
onverify: (flag: boolean) => void;
loggingService: LoggingService;
constructor(keyStore: MutableKeyStore) {
this.keyStore = keyStore;
this.onsign = (signature: Signature) => {};
this.onverify = (flag: boolean) => {};
}
public fingerprint(): string {
return this.keyStore.getFingerprint();
}
public prepare(material: Signable): boolean {
this.dgst = material.digest();
return true;
}
public verify(digest: string, signature: Signature): void {
openpgp.signature
.readArmored(signature.data)
.then((sig) => {
const opts = {
message: openpgp.cleartext.fromText(digest),
publicKeys: this.keyStore.getTrustedKeys(),
signature: sig,
};
openpgp.verify(opts).then((v) => {
let i = 0;
for (i = 0; i < v.signatures.length; i++) {
const s = v.signatures[i];
if (s.valid) {
this.onverify(s);
return;
}
}
this.loggingService.sendErrorLevelMessage(
`Checked ${i} signature(s) but none valid`,
this,
{ error: '404 Not found!' }
);
this.onverify(false);
});
})
.catch((e) => {
this.loggingService.sendErrorLevelMessage(e.message, this, { error: e });
this.onverify(false);
});
}
public async sign(digest: string): Promise<void> {
const m = openpgp.cleartext.fromText(digest);
const pk = this.keyStore.getPrivateKey();
if (!pk.isDecrypted()) {
const password = window.prompt('password');
await pk.decrypt(password);
}
const opts = {
message: m,
privateKeys: [pk],
detached: true,
};
openpgp
.sign(opts)
.then((s) => {
this.signature = {
engine: this.engine,
algo: this.algo,
data: s.signature,
// TODO: fix for browser later
digest,
};
this.onsign(this.signature);
})
.catch((e) => {
this.loggingService.sendErrorLevelMessage(e.message, this, { error: e });
this.onsign(undefined);
});
}
}
export { Signable, Signature, Signer, PGPSigner };