@cypher-swift/lib
v1.0.0
Published
A simple JS library for encrypting and decrypting binary data into Taylor Swift lyrics, using a pre-determined cipher key.
Downloads
1
Readme
@cypher-swift/lib
A simple JS library for encrypting and decrypting binary data into Taylor Swift lyrics, using a pre-determined cipher key.
Overview
This readme can be treated as a specification document for Cypher Swift. The library code has annotations to the relevant parts of the spec below for guidance. However let's be real, I only wrote this for my own sanity while building the project.
Knock yourselves out however, it's a real page turner.
Terminology
| term | definition | | ------------------- | -------------------------------------------------------------------------- | | lyric index | The index of a lyric in the master lyric file | | bespoke lyric index | The index of a lyric relating to the cipher key, not the master lyric file |
Magic bytes
| name | data type | value | description |
| ------------------------------- | --------- | ------ | ----------------------------------------------------- |
| cipher key (version 1) | uint8
| 0x13
| Indicates a package structure is a cipher key type |
| encrypted payload (version 1) | uint8
| 0x69
| Indicates a package structure is an encrypted payload |
Data structures
Cypher key structure
| name | data type | offset | description |
| -------------- | ---------- | ------ | ----------------------------------------------------------------- |
| padding length | uint8
| 0x00
| The amount of padding bits used at the end of the final bit array |
| magic number | uint8
| 0x01
| The magic number, used to validate the ciphered data |
| binary data | uint10[]
| 0x02
| The array of bespoke lyric indices |
Package structure
| name | data type | offset | description |
| -------------- | --------- | ------ | ----------------------------------------------------------------- |
| padding length | uint8
| 0x00
| The amount of padding bits used at the end of the final bit array |
| magic number | uint8
| 0x01
| The magic number, used to validate the ciphered data |
| binary data | uint8[]
| 0x02
| The array of binary data to be encrypted |
Encrypted structure
| name | data type | offset | description |
| ----------------- | ---------- | ------ | ------------------------- |
| lyric index array | uint10[]
| 0x00
| An array of lyric index's |
Functions
(1) Generate & encode a cipher key
- Parse the master list of Taylor Swift lyrics provided in this project into an array
- Clone the array of lyrics
- Shuffle the cloned array to generate new indices for each lyric
- Create a new bit array, which will be a cypher key structure
- Write an empty padding length byte (we'll determine the padding length later)
- Write the cipher key magic byte to the magic byte offset
- Write all of the bespoke lyric indices to the bit array
- Calculate padding length of cypher key structure data structure
8 - (((2 * 8) + (lyricCount * 10)) % 8)
wherelyricCount
should be 1024- Write this length as a byte to the padding length offset
- Write the amount of required random padding bits to the bit array
- Convert the bit array to a
Uint8Array
- Encode the
Uint8Array
to a url-websafe base64 string
(2) Decode a cipher key
- Accept a url-websafe base64 encoded string
- Decode the string into a
Uint8Array
- Read the padding byte
- Read and validate the magic byte
- Convert the
Uint8Array
to a bit array - Read though the bespoke lyric indices
- Use padding byte to know when to exit reading
- Parse the master list of Taylor Swift lyrics provided in this project into an array
- Read though bespoke lyric indices, use the master lyric list to get lyric values
- Return a
string[]
of lyrics to index
(3) Encrypt a payload
- Decode the inputted cipher key
- Accept an
Uint8Array
of binary data to be encrypted - Create a bit array, which will be a package structure
- Write an empty padding length byte (we'll determine the padding length later)
- Write the encrypted payload magic byte to the magic byte offset
- Write the inputted bytes from
3.1
- Calculate padding length of package structure
10 - (((2 * 8) + (length * 8)) % 10)
wherelength
is the length of the array from3.1
- Write this length as a byte to the padding length offset
- Write this amount of required random padding bits to the bit array
- Create an empty
string[]
to store the lyrics - Reset the offset position of the bit array to
0
- Read though the bit array again, one
uint10
at a time - Using the decoded cipher key, map the
uint10
value to a lyric - Add random line breaks every 5-7 lines (artificial verses)
- Read though the bit array again, one
- Return the lyrics as a string with formatting
(4) Decrypt a payload
- Decode the inputted cipher key
- Create inverse lookup map
- Accept a
string
containing the encrypted payload as Taylor Swift lyrics- Cleanup the string (trim whitespace, remove empty lines)
- Use the inverse lookup map to get the bespoke lyric indices
- Create a bit array
- Write all lyric index's to it as
uint10
- Write all lyric index's to it as
- Reset the offset position of the bit array to
0
- Read the padding byte
- Read and validate the magic byte
- Create a
Uint8Array
to store the extracted binary data - Seek to the start of the padding bits and set them all to
0
- Seek to the start of the package structure binary data
- Read though the data, using the padding byte to know to when to stop
- Decode the byte data into a utf-8 string
- Return the decrypted string