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

Encrypt decrypt with bytes #29

Closed
wants to merge 4 commits into from
Closed

Conversation

vijayshiyani
Copy link
Contributor

No description provided.

Copy link
Contributor

@zbarbuto zbarbuto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the changes look good - I think we just need a bit more general consistency with
stringAsBinaryBuffer
binaryToBytes/bytesToBinary (which is really misnamed and should be encodeByteString/decodeBinaryString )
util.binary.raw.encode

In reality we only really need 1 of these methods.

export const utf16ToBytes = util.text.utf16.encode;
export const bytesToUtf16 = util.text.utf16.decode;
export const binaryToBytes = util.binary.raw.decode;
export const bytesToBinary = util.binary.raw.encode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't like these two names - they aren't representative of what the function is actually doing. I think something like encodeByteString or binaryToString or something would make more sense.
However, I don't think they are required anyway - see other comments.

serializationVersion: SerializationFormat = SerializationFormat.latest_version
): Promise<IEncryptionResult & { generatedKey: string }> {
return encryptWithGeneratedKey(options, serializationVersion, 'raw');
return encryptWithGeneratedKey({ data: binaryToBytes(data), strategy, iv }, serializationVersion);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

binaryToBytes here should really be named decodeByteString for clarity

export function decryptBinaryWithKeyUsingArtefacts(
key: string,
encryptedData: any,
strategy: CipherStrategy,
{ iv, at, ad }: IEncryptionOptions
) {
return decryptWithKeyUsingArtefacts(key, encryptedData, strategy, { iv, at, ad }, 'raw');
const result = decryptWithKeyUsingArtefacts(key, encryptedData, strategy, { iv, at, ad });
return result ? bytesToBinary(result) : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think either rename this to decodeByteString or just use util.binary.raw.decode() directly since we're internal here so there's nothing wrong with just using raw node-forge methods.

if (!data || data === '') {
return { encrypted: null, serialized: null };
}
return encryptWithKey({ key, data: binaryToBytes(data), strategy, iv }, serializationVersion);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decodeByteString here - or just use the existing stringAsBinaryBuffer method that exists in the codebase.

@@ -266,33 +223,26 @@ export function encryptBinaryWithKeyUsingArtefacts(
encrypted: string | null;
artifacts?: any;
} {
return encryptWithKeyUsingArtefacts(key, data, strategy, iv, 'raw');
return encryptWithKeyUsingArtefacts({ key, data: binaryToBytes(data), strategy, iv });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decodeByteString here - or just use the existing stringAsBinaryBuffer method that exists in the codebase.

@@ -99,15 +100,11 @@ describe('Backwards and forwards copmatibility', () => {
// Prints as 'Helloøã'
const expected = decodeSafe64('SGVsbG_44wA=');

expect(decrypted).toEqual(expected);
expect(util.createBuffer(decrypted as Uint8Array).data).toEqual(expected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This typecast sees wrong. The result of decryptStringWithKey should be a string. If this is working I think this is a bug?

key,
});

expect(bytesToUtf8(decryptedWithSourceKey as Uint8Array)).toEqual(bytesToUtf8(data));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This typecast seems unnecessary. The result of decryptedWithSourceKey should be Promise<Uint8Array> so if the typecast is required here the typings are incorrect.

@zbarbuto
Copy link
Contributor

Superseded by #30 which has the above requested fixes included.

@zbarbuto zbarbuto closed this Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants