Add documentation to pgp module.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -64,6 +64,13 @@
|
||||
<code>src/app/_pgp/pgp-signer.ts</code>
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Description</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<p>Signable object interface </p>
|
||||
|
||||
</p>
|
||||
|
||||
|
||||
<section>
|
||||
@@ -115,14 +122,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="7"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:7</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="11"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:11</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>The message to be signed. </p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Returns : </b> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>
|
||||
@@ -138,56 +147,113 @@
|
||||
|
||||
|
||||
<div class="tab-pane fade tab-source-code" id="c-source">
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import * as openpgp from 'openpgp';
|
||||
|
||||
// Application imports
|
||||
import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
|
||||
import { LoggingService } from '@app/_services/logging.service';
|
||||
|
||||
const openpgp = require('openpgp');
|
||||
|
||||
/** Signable object interface */
|
||||
interface Signable {
|
||||
/** The message to be signed. */
|
||||
digest(): string;
|
||||
}
|
||||
|
||||
/** Signature object interface */
|
||||
interface Signature {
|
||||
/** Encryption engine used. */
|
||||
engine: string;
|
||||
/** Encryption algorithm used */
|
||||
algo: string;
|
||||
/** Data to be signed. */
|
||||
data: string;
|
||||
/** Message digest */
|
||||
digest: string;
|
||||
}
|
||||
|
||||
/** Signer interface */
|
||||
interface Signer {
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign(signature: Signature): void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify(flag: boolean): void;
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
fingerprint(): string;
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
prepare(material: Signable): boolean;
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
verify(digest: string, signature: Signature): void;
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
sign(digest: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** Provides functionality for signing and verifying signed messages. */
|
||||
class PGPSigner implements Signer {
|
||||
/** Encryption engine used. */
|
||||
engine = 'pgp';
|
||||
/** Encryption algorithm used */
|
||||
algo = 'sha256';
|
||||
/** Message digest */
|
||||
dgst: string;
|
||||
/** Generated signature */
|
||||
signature: Signature;
|
||||
/** A keystore holding pgp keys. */
|
||||
keyStore: MutableKeyStore;
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign: (signature: Signature) => void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify: (flag: boolean) => void;
|
||||
/** A service that provides logging capabilities. */
|
||||
loggingService: LoggingService;
|
||||
|
||||
/**
|
||||
* Initializing the Signer.
|
||||
* @param keyStore - A keystore holding pgp keys.
|
||||
*/
|
||||
constructor(keyStore: MutableKeyStore) {
|
||||
this.keyStore = keyStore;
|
||||
this.onsign = (signature: Signature) => {};
|
||||
this.onverify = (flag: boolean) => {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
public fingerprint(): string {
|
||||
return this.keyStore.getFingerprint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
public prepare(material: Signable): boolean {
|
||||
this.dgst = material.digest();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
public verify(digest: string, signature: Signature): void {
|
||||
openpgp.signature
|
||||
.readArmored(signature.data)
|
||||
@@ -220,6 +286,11 @@ class PGPSigner implements Signer {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
public async sign(digest: string): Promise<void> {
|
||||
const m = openpgp.cleartext.fromText(digest);
|
||||
const pk = this.keyStore.getPrivateKey();
|
||||
@@ -251,6 +322,7 @@ class PGPSigner implements Signer {
|
||||
}
|
||||
}
|
||||
|
||||
/** @exports */
|
||||
export { Signable, Signature, Signer, PGPSigner };
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
@@ -61,9 +61,16 @@
|
||||
<h3>File</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<code>src/app/_pgp/pgp-signer.ts</code>
|
||||
<code>src/app/_models/account.ts</code>
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Description</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<p>Meta signature interface </p>
|
||||
|
||||
</p>
|
||||
|
||||
|
||||
<section>
|
||||
@@ -128,6 +135,12 @@
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Algorithm used </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<table class="table table-sm table-bordered">
|
||||
@@ -157,6 +170,12 @@
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Data that was signed. </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<table class="table table-sm table-bordered">
|
||||
@@ -186,6 +205,12 @@
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Message digest </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<table class="table table-sm table-bordered">
|
||||
@@ -215,6 +240,12 @@
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Encryption engine used. </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
@@ -222,120 +253,149 @@
|
||||
|
||||
|
||||
<div class="tab-pane fade tab-source-code" id="c-source">
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">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,
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">interface AccountDetails {
|
||||
/** Account registration day */
|
||||
date_registered: number;
|
||||
/** User's gender */
|
||||
gender: string;
|
||||
/** Age of user */
|
||||
age?: string;
|
||||
/** Type of account */
|
||||
type?: string;
|
||||
/** Token balance on account */
|
||||
balance?: number;
|
||||
/** Account identifiers */
|
||||
identities: {
|
||||
evm: {
|
||||
'bloxberg:8996': string[];
|
||||
'oldchain:1': string[];
|
||||
};
|
||||
openpgp
|
||||
.sign(opts)
|
||||
.then((s) => {
|
||||
this.signature = {
|
||||
engine: this.engine,
|
||||
algo: this.algo,
|
||||
data: s.signature,
|
||||
// TODO: fix for browser later
|
||||
digest,
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
};
|
||||
/** User's location */
|
||||
location: {
|
||||
area?: string;
|
||||
area_name: string;
|
||||
area_type?: string;
|
||||
};
|
||||
/** Products or services provided by user. */
|
||||
products: string[];
|
||||
/** Business category of user. */
|
||||
category?: string;
|
||||
/** Personal identifying information of user */
|
||||
vcard: {
|
||||
email: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
fn: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
n: [
|
||||
{
|
||||
value: string[];
|
||||
}
|
||||
];
|
||||
tel: [
|
||||
{
|
||||
meta: {
|
||||
TYP: string[];
|
||||
};
|
||||
this.onsign(this.signature);
|
||||
})
|
||||
.catch((e) => {
|
||||
this.loggingService.sendErrorLevelMessage(e.message, this, { error: e });
|
||||
this.onsign(undefined);
|
||||
});
|
||||
}
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
version: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
};
|
||||
}
|
||||
|
||||
export { Signable, Signature, Signer, PGPSigner };
|
||||
/** Meta signature interface */
|
||||
interface Signature {
|
||||
/** Algorithm used */
|
||||
algo: string;
|
||||
/** Data that was signed. */
|
||||
data: string;
|
||||
/** Message digest */
|
||||
digest: string;
|
||||
/** Encryption engine used. */
|
||||
engine: string;
|
||||
}
|
||||
|
||||
/** Meta object interface */
|
||||
interface Meta {
|
||||
/** Account details */
|
||||
data: AccountDetails;
|
||||
/** Meta store id */
|
||||
id: string;
|
||||
/** Signature used during write. */
|
||||
signature: Signature;
|
||||
}
|
||||
|
||||
/** Meta response interface */
|
||||
interface MetaResponse {
|
||||
/** Meta store id */
|
||||
id: string;
|
||||
/** Meta object */
|
||||
m: Meta;
|
||||
}
|
||||
|
||||
/** Default account data object */
|
||||
const defaultAccount: AccountDetails = {
|
||||
date_registered: Date.now(),
|
||||
gender: 'other',
|
||||
identities: {
|
||||
evm: {
|
||||
'bloxberg:8996': [''],
|
||||
'oldchain:1': [''],
|
||||
},
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
},
|
||||
location: {
|
||||
area_name: 'Kilifi',
|
||||
},
|
||||
products: [],
|
||||
vcard: {
|
||||
email: [
|
||||
{
|
||||
value: '',
|
||||
},
|
||||
],
|
||||
fn: [
|
||||
{
|
||||
value: 'Sarafu Contract',
|
||||
},
|
||||
],
|
||||
n: [
|
||||
{
|
||||
value: ['Sarafu', 'Contract'],
|
||||
},
|
||||
],
|
||||
tel: [
|
||||
{
|
||||
meta: {
|
||||
TYP: [],
|
||||
},
|
||||
value: '',
|
||||
},
|
||||
],
|
||||
version: [
|
||||
{
|
||||
value: '3.0',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
/** @exports */
|
||||
export { AccountDetails, Meta, MetaResponse, Signature, defaultAccount };
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -61,14 +61,14 @@
|
||||
<h3>File</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<code>src/app/_models/account.ts</code>
|
||||
<code>src/app/_pgp/pgp-signer.ts</code>
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Description</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<p>Meta signature interface </p>
|
||||
<p>Signature object interface </p>
|
||||
|
||||
</p>
|
||||
|
||||
@@ -137,7 +137,7 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Algorithm used </p>
|
||||
<div class="io-description"><p>Encryption algorithm used </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -172,7 +172,7 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Data that was signed. </p>
|
||||
<div class="io-description"><p>Data to be signed. </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -253,149 +253,183 @@
|
||||
|
||||
|
||||
<div class="tab-pane fade tab-source-code" id="c-source">
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">interface AccountDetails {
|
||||
/** Account registration day */
|
||||
date_registered: number;
|
||||
/** User's gender */
|
||||
gender: string;
|
||||
/** Age of user */
|
||||
age?: string;
|
||||
/** Type of account */
|
||||
type?: string;
|
||||
/** Token balance on account */
|
||||
balance?: number;
|
||||
/** Account identifiers */
|
||||
identities: {
|
||||
evm: {
|
||||
'bloxberg:8996': string[];
|
||||
'oldchain:1': string[];
|
||||
};
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
};
|
||||
/** User's location */
|
||||
location: {
|
||||
area?: string;
|
||||
area_name: string;
|
||||
area_type?: string;
|
||||
};
|
||||
/** Products or services provided by user. */
|
||||
products: string[];
|
||||
/** Business category of user. */
|
||||
category?: string;
|
||||
/** Personal identifying information of user */
|
||||
vcard: {
|
||||
email: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
fn: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
n: [
|
||||
{
|
||||
value: string[];
|
||||
}
|
||||
];
|
||||
tel: [
|
||||
{
|
||||
meta: {
|
||||
TYP: string[];
|
||||
};
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
version: [
|
||||
{
|
||||
value: string;
|
||||
}
|
||||
];
|
||||
};
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import * as openpgp from 'openpgp';
|
||||
|
||||
// Application imports
|
||||
import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
|
||||
import { LoggingService } from '@app/_services/logging.service';
|
||||
|
||||
/** Signable object interface */
|
||||
interface Signable {
|
||||
/** The message to be signed. */
|
||||
digest(): string;
|
||||
}
|
||||
|
||||
/** Meta signature interface */
|
||||
/** Signature object interface */
|
||||
interface Signature {
|
||||
/** Algorithm used */
|
||||
/** Encryption engine used. */
|
||||
engine: string;
|
||||
/** Encryption algorithm used */
|
||||
algo: string;
|
||||
/** Data that was signed. */
|
||||
/** Data to be signed. */
|
||||
data: string;
|
||||
/** Message digest */
|
||||
digest: string;
|
||||
}
|
||||
|
||||
/** Signer interface */
|
||||
interface Signer {
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign(signature: Signature): void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify(flag: boolean): void;
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
fingerprint(): string;
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
prepare(material: Signable): boolean;
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
verify(digest: string, signature: Signature): void;
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
sign(digest: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** Provides functionality for signing and verifying signed messages. */
|
||||
class PGPSigner implements Signer {
|
||||
/** Encryption engine used. */
|
||||
engine: string;
|
||||
}
|
||||
|
||||
/** Meta object interface */
|
||||
interface Meta {
|
||||
/** Account details */
|
||||
data: AccountDetails;
|
||||
/** Meta store id */
|
||||
id: string;
|
||||
/** Signature used during write. */
|
||||
engine = 'pgp';
|
||||
/** Encryption algorithm used */
|
||||
algo = 'sha256';
|
||||
/** Message digest */
|
||||
dgst: string;
|
||||
/** Generated signature */
|
||||
signature: Signature;
|
||||
}
|
||||
/** A keystore holding pgp keys. */
|
||||
keyStore: MutableKeyStore;
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign: (signature: Signature) => void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify: (flag: boolean) => void;
|
||||
/** A service that provides logging capabilities. */
|
||||
loggingService: LoggingService;
|
||||
|
||||
/** Meta response interface */
|
||||
interface MetaResponse {
|
||||
/** Meta store id */
|
||||
id: string;
|
||||
/** Meta object */
|
||||
m: Meta;
|
||||
}
|
||||
/**
|
||||
* Initializing the Signer.
|
||||
* @param keyStore - A keystore holding pgp keys.
|
||||
*/
|
||||
constructor(keyStore: MutableKeyStore) {
|
||||
this.keyStore = keyStore;
|
||||
this.onsign = (signature: Signature) => {};
|
||||
this.onverify = (flag: boolean) => {};
|
||||
}
|
||||
|
||||
/** Default account data object */
|
||||
const defaultAccount: AccountDetails = {
|
||||
date_registered: Date.now(),
|
||||
gender: 'other',
|
||||
identities: {
|
||||
evm: {
|
||||
'bloxberg:8996': [''],
|
||||
'oldchain:1': [''],
|
||||
},
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
},
|
||||
location: {
|
||||
area_name: 'Kilifi',
|
||||
},
|
||||
products: [],
|
||||
vcard: {
|
||||
email: [
|
||||
{
|
||||
value: '',
|
||||
},
|
||||
],
|
||||
fn: [
|
||||
{
|
||||
value: 'Sarafu Contract',
|
||||
},
|
||||
],
|
||||
n: [
|
||||
{
|
||||
value: ['Sarafu', 'Contract'],
|
||||
},
|
||||
],
|
||||
tel: [
|
||||
{
|
||||
meta: {
|
||||
TYP: [],
|
||||
},
|
||||
value: '',
|
||||
},
|
||||
],
|
||||
version: [
|
||||
{
|
||||
value: '3.0',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
public fingerprint(): string {
|
||||
return this.keyStore.getFingerprint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
public prepare(material: Signable): boolean {
|
||||
this.dgst = material.digest();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** @exports */
|
||||
export { AccountDetails, Meta, MetaResponse, Signature, defaultAccount };
|
||||
export { Signable, Signature, Signer, PGPSigner };
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -64,6 +64,13 @@
|
||||
<code>src/app/_pgp/pgp-signer.ts</code>
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Description</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<p>Signer interface </p>
|
||||
|
||||
</p>
|
||||
|
||||
|
||||
<section>
|
||||
@@ -130,19 +137,27 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="20"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:20</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="36"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:36</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Get the private key fingerprint.</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
</div>
|
||||
<div class="io-description">
|
||||
<b>Returns : </b> <code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string" target="_blank" >string</a></code>
|
||||
|
||||
</div>
|
||||
<div class="io-description">
|
||||
<p>A private key fingerprint.</p>
|
||||
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
@@ -169,14 +184,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="18"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:18</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="29"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:29</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Event triggered on successful signing of message. </p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Parameters :</b>
|
||||
@@ -239,14 +256,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="19"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:19</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="31"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:31</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Event triggered on successful verification of a signature. </p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Parameters :</b>
|
||||
@@ -309,14 +328,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="21"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:21</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="42"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:42</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Load the message digest.</p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Parameters :</b>
|
||||
@@ -326,6 +347,7 @@
|
||||
<td>Name</td>
|
||||
<td>Type</td>
|
||||
<td>Optional</td>
|
||||
<td>Description</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -340,6 +362,12 @@
|
||||
</td>
|
||||
|
||||
|
||||
<td>
|
||||
<ul>
|
||||
<li>A signable object.</li>
|
||||
</ul>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -351,7 +379,8 @@
|
||||
|
||||
</div>
|
||||
<div class="io-description">
|
||||
|
||||
<p>true - If digest has been loaded successfully.</p>
|
||||
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -379,14 +408,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="23"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:23</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="54"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:54</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Signs a message using a private key.</p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Parameters :</b>
|
||||
@@ -396,6 +427,7 @@
|
||||
<td>Name</td>
|
||||
<td>Type</td>
|
||||
<td>Optional</td>
|
||||
<td>Description</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -410,6 +442,12 @@
|
||||
</td>
|
||||
|
||||
|
||||
<td>
|
||||
<ul>
|
||||
<li>The message to be signed.</li>
|
||||
</ul>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -449,14 +487,16 @@
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="22"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:22</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="48"
|
||||
class="link-to-prism">src/app/_pgp/pgp-signer.ts:48</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Verify that signature is valid.</p>
|
||||
</div>
|
||||
|
||||
<div class="io-description">
|
||||
<b>Parameters :</b>
|
||||
@@ -466,6 +506,7 @@
|
||||
<td>Name</td>
|
||||
<td>Type</td>
|
||||
<td>Optional</td>
|
||||
<td>Description</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -480,6 +521,12 @@
|
||||
</td>
|
||||
|
||||
|
||||
<td>
|
||||
<ul>
|
||||
<li>The message that was signed.</li>
|
||||
</ul>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>signature</td>
|
||||
@@ -492,6 +539,12 @@
|
||||
</td>
|
||||
|
||||
|
||||
<td>
|
||||
<ul>
|
||||
<li>The generated signature.</li>
|
||||
</ul>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -515,56 +568,113 @@
|
||||
|
||||
|
||||
<div class="tab-pane fade tab-source-code" id="c-source">
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import * as openpgp from 'openpgp';
|
||||
|
||||
// Application imports
|
||||
import { MutableKeyStore } from '@app/_pgp/pgp-key-store';
|
||||
import { LoggingService } from '@app/_services/logging.service';
|
||||
|
||||
const openpgp = require('openpgp');
|
||||
|
||||
/** Signable object interface */
|
||||
interface Signable {
|
||||
/** The message to be signed. */
|
||||
digest(): string;
|
||||
}
|
||||
|
||||
/** Signature object interface */
|
||||
interface Signature {
|
||||
/** Encryption engine used. */
|
||||
engine: string;
|
||||
/** Encryption algorithm used */
|
||||
algo: string;
|
||||
/** Data to be signed. */
|
||||
data: string;
|
||||
/** Message digest */
|
||||
digest: string;
|
||||
}
|
||||
|
||||
/** Signer interface */
|
||||
interface Signer {
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign(signature: Signature): void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify(flag: boolean): void;
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
fingerprint(): string;
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
prepare(material: Signable): boolean;
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
verify(digest: string, signature: Signature): void;
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
sign(digest: string): Promise<void>;
|
||||
}
|
||||
|
||||
/** Provides functionality for signing and verifying signed messages. */
|
||||
class PGPSigner implements Signer {
|
||||
/** Encryption engine used. */
|
||||
engine = 'pgp';
|
||||
/** Encryption algorithm used */
|
||||
algo = 'sha256';
|
||||
/** Message digest */
|
||||
dgst: string;
|
||||
/** Generated signature */
|
||||
signature: Signature;
|
||||
/** A keystore holding pgp keys. */
|
||||
keyStore: MutableKeyStore;
|
||||
/** Event triggered on successful signing of message. */
|
||||
onsign: (signature: Signature) => void;
|
||||
/** Event triggered on successful verification of a signature. */
|
||||
onverify: (flag: boolean) => void;
|
||||
/** A service that provides logging capabilities. */
|
||||
loggingService: LoggingService;
|
||||
|
||||
/**
|
||||
* Initializing the Signer.
|
||||
* @param keyStore - A keystore holding pgp keys.
|
||||
*/
|
||||
constructor(keyStore: MutableKeyStore) {
|
||||
this.keyStore = keyStore;
|
||||
this.onsign = (signature: Signature) => {};
|
||||
this.onverify = (flag: boolean) => {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the private key fingerprint.
|
||||
* @returns A private key fingerprint.
|
||||
*/
|
||||
public fingerprint(): string {
|
||||
return this.keyStore.getFingerprint();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the message digest.
|
||||
* @param material - A signable object.
|
||||
* @returns true - If digest has been loaded successfully.
|
||||
*/
|
||||
public prepare(material: Signable): boolean {
|
||||
this.dgst = material.digest();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that signature is valid.
|
||||
* @param digest - The message that was signed.
|
||||
* @param signature - The generated signature.
|
||||
*/
|
||||
public verify(digest: string, signature: Signature): void {
|
||||
openpgp.signature
|
||||
.readArmored(signature.data)
|
||||
@@ -597,6 +707,11 @@ class PGPSigner implements Signer {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a message using a private key.
|
||||
* @async
|
||||
* @param digest - The message to be signed.
|
||||
*/
|
||||
public async sign(digest: string): Promise<void> {
|
||||
const m = openpgp.cleartext.fromText(digest);
|
||||
const pk = this.keyStore.getPrivateKey();
|
||||
@@ -628,6 +743,7 @@ class PGPSigner implements Signer {
|
||||
}
|
||||
}
|
||||
|
||||
/** @exports */
|
||||
export { Signable, Signature, Signer, PGPSigner };
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user