In the real situation, information security is important and we may want to send encrypted text to user for their credential. It is good for validation and it can prevent some attack from user. Cloudflare worker
has an implemented encode and decode feature, so it is possible to encrypt and deliver message to user via a constraint distributed system. The pointing system of https://capitaltwo.ga
is built based on this. More details come below:
Details about different possible configuration could check official website at Web Crypto
Encode
Code as below, comment is the description of purpose of each line:
1 | async function encode(text, password) { |
In order to coded to a thing that could be delivered and saved in a normal text environment (in the last seven lines of the above coded), we can convert ArrayBuffer
to Uint8Array
and then use JSON.stringify
to parse it. Like:
1 | /** |
The encryption process is a Promise and should be handled with
await
Decode
In order to decode, we need to recover generated base64 encoded text to normal buffer at first, like this:
1 | /** |
Other codes as below, comment is the description of purpose of each line:
1 | /** |
Compare ArrayBuffer
The easiest way to compare is through compare the digit one by one, here is a sample:
1 | function equal(buf1, buf2) { |