Skip to content

Commit

Permalink
simplify utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Jan 10, 2019
1 parent 0188dd6 commit 9df4201
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 104 deletions.
37 changes: 9 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,12 @@ All methods are asynchronous and return promises (except for convert utils)
- verify(data: string, secretToVerify: string, hash: string)
- utils
- randomBytes(bytes: number)
- convert
- ArrayBuffer
- to
- Utf8(input: ArrayBuffer)
- Hex(input: ArrayBuffer)
- Base64(input: ArrayBuffer)
- from
- Utf8(input: string)
- Hex(input: string)
- Base64(input: string)
- Utf8
- to
- ArrayBuffer(input: string)
- from
- ArrayBuffer(input: ArrayBuffer)
- Hex
- to
- ArrayBuffer(input: string)
- from
- ArrayBuffer(input: ArrayBuffer)
- Base64
- to
- ArrayBuffer(input: string)
- from
- ArrayBuffer(input: ArrayBuffer)
- convertArrayBufferToUtf8(input: ArrayBuffer)
- convertUtf8ToArrayBuffer(input: string)
- convertArrayBufferToBase64(input: ArrayBuffer)
- convertBase64ToArrayBuffer(input: string)
- convertArrayBufferToHex(input: ArrayBuffer)
- convertHexToArrayBuffer(input: string)
```

> _NOTE:_ Supported hashing algorithms for RSA and PBKDF2 are:
Expand All @@ -135,7 +116,7 @@ import RNSimpleCrypto from "react-native-simple-crypto";
// -- AES ------------------------------------------------------------- //

const message = "data to encrypt";
const messageArrayBuffer = RNSimpleCrypto.utils.convert.Utf8.to.ArrayBuffer(
const messageArrayBuffer = RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(
message
);

Expand All @@ -157,7 +138,7 @@ const messageArrayBuffer = await RNSimpleCrypto.AES.decrypt(
keyArrayBuffer,
ivArrayBuffer
);
const message = RNSimpleCrypto.utils.convert.ArrayBuffer.to.Utf8(
const message = RNSimpleCrypto.utils.convertArrayBufferToUtf8(
messageArrayBuffer
);
console.log("AES decrypt", message);
Expand All @@ -166,7 +147,7 @@ console.log("AES decrypt", message);

const signatureArrayBuffer = await RNSimpleCrypto.HMAC.hmac256(message, key);

const signatureHex = RNSimpleCrypto.utils.convert.ArrayBuffer.to.Hex(
const signatureHex = RNSimpleCrypto.utils.convertArrayBuffertoHex(
signatureArrayBuffer
);
console.log("HMAC signature", signatureHex);
Expand Down
44 changes: 6 additions & 38 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,11 @@ declare module "react-native-simple-crypto" {

export namespace utils {
export function randomBytes(bytes: number): Promise<ArrayBuffer>;
export interface convert {
ArrayBuffer: {
to: {
Utf8: (arrayBuffer: ArrayBuffer) => string;
Hex: (arrayBuffer: ArrayBuffer) => string;
Base64: (arrayBuffer: ArrayBuffer) => string;
};
from: {
Utf8: (string: string) => ArrayBuffer;
Hex: (string: string) => ArrayBuffer;
Base64: (string: string) => ArrayBuffer;
};
};
Utf8: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
Hex: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
Base64: {
to: {
ArrayBuffer: (string: string) => ArrayBuffer;
};
from: {
ArrayBuffer: (arrayBuffer: ArrayBuffer) => string;
};
};
}
export function convertArrayBufferToUtf8(input: ArrayBuffer): string;
export function convertUtf8ToArrayBuffer(input: string): ArrayBuffer;
export function convertArrayBufferToBase64(input: ArrayBuffer): string;
export function convertBase64ToArrayBuffer(input: string): ArrayBuffer;
export function convertArrayBufferToHex(input: ArrayBuffer): string;
export function convertHexToArrayBuffer(input: string): ArrayBuffer;
}
}
44 changes: 6 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,44 +159,12 @@ const RSA = NativeModules.Rsa;

const utils = {
randomBytes: randomBytes,
convert: {
ArrayBuffer: {
to: {
Utf8: convertArrayBufferToUtf8,
Hex: convertArrayBufferToHex,
Base64: convertArrayBufferToBase64
},
from: {
Utf8: convertUtf8ToArrayBuffer,
Hex: convertHexToArrayBuffer,
Base64: convertBase64ToArrayBuffer
}
},
Utf8: {
to: {
ArrayBuffer: convertUtf8ToArrayBuffer
},
from: {
ArrayBuffer: convertArrayBufferToUtf8
}
},
Hex: {
to: {
ArrayBuffer: convertHexToArrayBuffer
},
from: {
ArrayBuffer: convertArrayBufferToHex
}
},
Base64: {
to: {
ArrayBuffer: convertBase64ToArrayBuffer
},
from: {
ArrayBuffer: convertArrayBufferToBase64
}
}
}
convertArrayBufferToUtf8: convertArrayBufferToUtf8,
convertUtf8ToArrayBuffer: convertUtf8ToArrayBuffer,
convertArrayBufferToBase64: convertArrayBufferToBase64,
convertBase64ToArrayBuffer: convertBase64ToArrayBuffer,
convertArrayBufferToHex: convertArrayBufferToHex,
convertHexToArrayBuffer: convertHexToArrayBuffer
};

export default {
Expand Down

0 comments on commit 9df4201

Please sign in to comment.