Skip to content

Commit

Permalink
refactor: encrypt/decrypt
Browse files Browse the repository at this point in the history
update names of encryp/decrypt and encryptor/decryptor methods to match convention
update
tests
update docs

BREAKING CHANGE: encode/decode methods names changed to encrypt/decrypt
encoder\decoder methods
names changed to encryptor/decryptor

fix VK-10
  • Loading branch information
uamanager committed Aug 26, 2019
1 parent 1bd5c0f commit e23683a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 90 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ And vice versa.
To mark object as private just add `private: true` property.
Example:
```typescript
// this object won't be encoded
// this object won't be encrypted
const example1 = {
foo: 'bar'
};
Expand All @@ -50,7 +50,7 @@ And vice versa.
};
```

### <a name="encode" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L49">`encode`</a>
### <a name="encrypt" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L49">`encrypt`</a>
Main method for encryption of object by protocol.
Examples:

Expand Down Expand Up @@ -135,11 +135,11 @@ const encoded = qcp.encode(example);
// }
```

### <a name="decode" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L178">`decode`</a>
Main method for decryption of object by protocol. Works the same as `encode`
### <a name="decrypt" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L178">`decrypt`</a>
Main method for decryption of object by protocol. Works the same as `encrypt`
method but in reverse order. Not throwing error if passed data is not by protocol.

### <a name="encoder/decoder" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L201">`encoder/decoder`</a>
### <a name="encryptor/decryptor" href="https://github.com/uamanager/qcp/blob/master/src/protocol.ts#L201">`encryptor/decryptor`</a>
Methods that should be overwritten to support encryption.
Example:
```typescript
Expand All @@ -151,14 +151,14 @@ export class Protocol extends QuickCrypticoProtocol {
super();
}

// `encoder` - method for data encrypt.
// `encryptor` - method for data encrypt.
// By default returns the same data as was passed.
public encoder (data: string) {
public encryptor (data: string) {
return cryptico.encrypt(data, this.publicKey).cipher;
}
// `decoder` - method for data decrypt.
// `decryptor` - method for data decrypt.
// By default returns the same data as was passed.
public encoder (data: string) {
public decryptor (data: string) {
return cryptico.decrypt(data, this.privateKey).plaintext;
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {Id} from './id';
// To mark object as private just add `private: true` property.
// Example:
// ```typescript
// // this object won't be encoded
// // this object won't be encrypted
// const example1 = {
// foo: 'bar'
// };
Expand All @@ -46,10 +46,10 @@ export class QuickCrypticoProtocol {

constructor () {}

//# encode
//# encrypt
// Main method for encryption of object by protocol.
// Examples:
public encode (data: any, name?: string) {
public encrypt (data: any, name?: string) {
let result: { public: any, private?: any } = {
public: data,
private: {}
Expand Down Expand Up @@ -92,7 +92,7 @@ export class QuickCrypticoProtocol {
// ```
result.public = [...result.public];
result.public = result.public.map((value, index) => {
const tmp = this.encode(value, index.toString());
const tmp = this.encrypt(value, index.toString());
result.private = {
...result.private,
...tmp.private
Expand Down Expand Up @@ -154,7 +154,7 @@ export class QuickCrypticoProtocol {
// ```
Object.keys(result.public)
.forEach((key) => {
const tmp = this.encode(result.public[key], key);
const tmp = this.encrypt(result.public[key], key);
result.private = {
...result.private,
...tmp.private
Expand All @@ -166,7 +166,7 @@ export class QuickCrypticoProtocol {

if (!name) {
if (Object.keys(result.private).length) {
result.private = this.encoder(this.serialize(result.private));
result.private = this.encryptor(this.serialize(result.private));
} else {
delete result.private;
}
Expand All @@ -175,14 +175,14 @@ export class QuickCrypticoProtocol {
return result;
}

//# decode
// Main method for decryption of object by protocol. Works the same as `encode`
//# decrypt
// Main method for decryption of object by protocol. Works the same as `encrypt`
// method but in reverse order. Not throwing error if passed data is not by protocol.
public decode (data) {
public decrypt (data) {
if (data instanceof Object && data.hasOwnProperty('public')) {
if (data.hasOwnProperty('private')) {
let _result = JSON.stringify(data.public);
const _private = this.deserialize(this.decoder(data.private));
const _private = this.deserialize(this.decryptor(data.private));

Object.keys(_private)
.forEach((key) => {
Expand All @@ -198,7 +198,7 @@ export class QuickCrypticoProtocol {
}
}

//# encoder/decoder
//# encryptor/decryptor
// Methods that should be overwritten to support encryption.
// Example:
// ```typescript
Expand All @@ -210,23 +210,23 @@ export class QuickCrypticoProtocol {
// super();
// }
//
// // `encoder` - method for data encrypt.
// // `encryptor` - method for data encrypt.
// // By default returns the same data as was passed.
// public encoder (data: string) {
// public encryptor (data: string) {
// return cryptico.encrypt(data, this.publicKey).cipher;
// }
// // `decoder` - method for data decrypt.
// // `decryptor` - method for data decrypt.
// // By default returns the same data as was passed.
// public encoder (data: string) {
// public decryptor (data: string) {
// return cryptico.decrypt(data, this.privateKey).plaintext;
// }
// }
// ```
public encoder (data: string) {
public encryptor (data: string) {
return data;
}

public decoder (data: string) {
public decryptor (data: string) {
return data;
}

Expand Down
Loading

0 comments on commit e23683a

Please sign in to comment.