Documentation ¶
Overview ¶
Package vigenere provides a secure symmetric encryption implementation with Vigenère-Vernam Cipher algorithm. The Vigenère Cipher in this library also acts as a One-Time Pad (OTP) for additional security. Vigenère Cipher is done by defining a character set or alphabets to be used. Then, we use a truly random number (in this library, pseudorandom number generator is used). Sum it with the numerical equivalent of our plaintext. Numerical equivalent in this library refers to the index in a slice in which the alphabet is stored. After that, the sum of the plaintext and the random number is modulo'ed by the length of our alphabets.
The plaintext and secret itself must conform to the alphabets defined beforehand, or the encryption process will ignore the alphabet (may cause errors). In this library, there is a function named `ValidateString` which will validate whether the plaintext inserted into the function is correct or not. There should also not be any duplicates in the character set. The default character set / alphabets is A to Z (uppercase only, with index of the slice starting from zero to 25).
This library attempts its best to conform to SRP (Single Responsibility Principle). Basically, this library expects you to understand the process (what to do) when performing encryption or decryption, but in most cases, all you need to do is (example for encryption):
- Instantiate a new `Vigenere` struct.
- Receive a plaintext from the user, verify if the plaintext conforms to the character set / alphabets.
- Ensure that the secret given by the user is equal in length with the plaintext, if not, we can generate our own secrets.
- Encrypt the data.
- Done, and you'll get the ciphertext.
- You are free to use the output in any form as you see fit: UTF-8, Base32, Base64, Base16 (Hex), Buffer/Bytes, or anything.
To check the implementation as a console application, see example at `example/main.go`.
Index ¶
- func GenerateDefaultAlphabets() []string
- type Vigenere
- func (v *Vigenere) ConvertToNumeric(str string) []int64
- func (v *Vigenere) ConvertToString(numbers []int64) string
- func (v *Vigenere) Decrypt(ciphertext, secret string) (string, error)
- func (v *Vigenere) Encrypt(plaintext, secret string) (string, error)
- func (v *Vigenere) GenerateRandomNumber() (int64, error)
- func (v *Vigenere) GenerateSecretKey(plaintext string) (string, error)
- func (v *Vigenere) ValidateString(str string) (bool, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateDefaultAlphabets ¶
func GenerateDefaultAlphabets() []string
GenerateDefaultAlphabets generates alphabets from 'A' to 'Z', or characters with code 65 to 90 in ASCII format. The `i` variable is transformed from `rune` to `string` for compatibility.
Types ¶
type Vigenere ¶
type Vigenere struct { Alphabets []string // List of alphabets or characters to be used for the algorithm. The numerical equivalent of the alphabet is the slice index. Do not input duplicate characters. Data type is not `rune` for interoperability (Go internally uses UTF-8 encoding). Length int64 // Length of `Alphabets` slice. Automatically created in order to not waste time and space in various methods in which this attribute is used. RandomSource io.Reader // Source of the random number generator. Automatically set to `rand.Reader` in order to use `crypto/rand` module for pseudorandom number generation. }
Vigenere provides a struct to hold the required alphabets, its length, and its source of randomness.
func NewVigenere ¶
NewVigenere creates a new instance of `Vigenere`, along with its methods. If you desire to use the default alphabets, pass `nil` or `{}string[]` as the argument. This method will throw an error if you pass any duplicate alphabets (passing 'A' and 'A' for example).
func (*Vigenere) ConvertToNumeric ¶
ConvertToNumber converts a string into their numeric equivalent. The numeric equivalent is found on `Alphabets` slice.
func (*Vigenere) ConvertToString ¶
ConvertToString converts an int64 slice to their alphabet equivalent, and transforms them to a single string, ready to be used. Essentially, the slice index is the numerical equivalent of an alphabet.
func (*Vigenere) Decrypt ¶
Decrypt decrypts a ciphertext with a secret key. Make sure that the secret key is equal in length with the ciphertext. Returns the plaintext.
func (*Vigenere) Encrypt ¶
Encrypt encrypts a plaintext with a secret key. Make sure that the secret key is equal in length with the plaintext. Returns the ciphertext.
func (*Vigenere) GenerateRandomNumber ¶
GenerateRandomNumber securely generates a random number (index) from zero to the max length of the `Alphabets` slice. Function `rand.Int` is exclusive (from 0 to n - 1) where `n` can be of any length.
func (*Vigenere) GenerateSecretKey ¶
GenerateSecretKey generates a secure secret that is equal in length with the plaintext. This is done to ensure the security of the encryption. Complexity is O(N).
func (*Vigenere) ValidateString ¶
ValidateString validates a string whether it conforms to the required alphabets or not. The process transforms the `Alphabets` slice into a hash map / object / map, and then checks the availability of each letters.