monocypher

package
Version: v0.0.0-...-243209b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AeadLock

func AeadLock(plaintext, nonce, key, addData []byte) (mac, ciphertext, data []byte)

AeadLock is the same as Lock() but allows some additional data to be signed though not encrypted with the rest. AeadLock returns mac, ciphertext and the authenticated text.

Example
key := []byte("Gophers are such a fun thing. Hehehehehe")
// Record of nonce generated by crypto/rand.Read()
nonce := []byte{107, 131, 11, 111, 188, 30, 66, 190, 31, 157, 156, 136, 226, 107, 32, 114, 231, 159, 140, 245, 164, 170, 242, 191}
plain := []byte("Hello this is Gopher army, we are gonna rule this world ")
ad := []byte("hi there")
mac, cipher, add := AeadLock(plain, nonce, key, ad)
fmt.Println("MAC", mac, "Cipher", cipher, "ADD", add)
Output:

MAC [101 221 188 62 235 121 90 252 179 62 127 56 61 130 102 235] Cipher [62 210 135 119 146 59 215 233 197 251 31 141 50 104 181 169 2 21 19 227 29 85 158 85 244 21 46 124 232 46 171 148 127 92 227 129 227 68 181 49 125 91 206 210 81 115 140 66 154 186 195 181 254 142 202 145] ADD [104 105 32 116 104 101 114 101]

func AeadUnlock

func AeadUnlock(ciphertext, nonce, key, mac, addData []byte) (plaintext []byte)

AeadUnlock is the same as Unlock(), but checks authenticated data and returns the deciphered plaintext.

Example
key := []byte("Gophers are such a fun thing. Hehehehehe")
nonce := []byte{107, 131, 11, 111, 188, 30, 66, 190, 31, 157, 156, 136, 226, 107, 32, 114, 231, 159, 140, 245, 164, 170, 242, 191}
mac := []byte{101, 221, 188, 62, 235, 121, 90, 252, 179, 62, 127, 56, 61, 130, 102, 235}
cipher := []byte{62, 210, 135, 119, 146, 59, 215, 233, 197, 251, 31, 141, 50, 104, 181, 169, 2, 21, 19, 227, 29, 85, 158, 85, 244, 21, 46, 124, 232, 46, 171, 148, 127, 92, 227, 129, 227, 68, 181, 49, 125, 91, 206, 210, 81, 115, 140, 66, 154, 186, 195, 181, 254, 142, 202, 145}
add := []byte{104, 105, 32, 116, 104, 101, 114, 101}
plain := AeadUnlock(cipher, nonce, key, mac, add)
fmt.Println(string(plain))
Output:

Hello this is Gopher army, we are gonna rule this world

func CheckMessageSignature

func CheckMessageSignature(message, publicKey, signature []byte) (result bool)

CheckSign checks the message and its corresponding public key and signature for validity.

Example
message := []byte("Hello this is Gopher army, we are gonna rule this world ")
publicKey := []byte{224, 191, 49, 187, 175, 40, 57, 91, 163, 14, 204, 126, 226, 196, 209, 163, 20, 55, 100, 80, 214, 24, 49, 221, 59, 62, 177, 82, 191, 215, 161, 202}
signature := []byte{64, 83, 187, 13, 39, 201, 125, 215, 140, 212, 100, 97, 25, 240, 239, 101, 170, 124, 81, 142, 173, 70, 89, 232, 17, 124, 95, 189, 39, 182, 66, 111, 153, 207, 243, 14, 148, 184, 184, 56, 135, 72, 37, 141, 143, 144, 199, 167, 211, 78, 36, 123, 9, 142, 40, 236, 178, 110, 81, 169, 156, 117, 202, 5}
result := CheckMessageSignature(message, publicKey, signature)
fmt.Println(result)
Output:

true

func GenPublicKey

func GenPublicKey(secretKey []byte) (publicKey []byte)

SignPublicKey is blake2b based curve25519 public key generator meant for signing messages alone.

Example
secretKey := []byte("Gophers are such a fun thing. Hehehehehe")
publicKey := GenPublicKey(secretKey)
// fmt.Println(base32.StdEncoding.EncodeToString(publicKey))
fmt.Println(publicKey)
Output:

[224 191 49 187 175 40 57 91 163 14 204 126 226 196 209 163 20 55 100 80 214 24 49 221 59 62 177 82 191 215 161 202]

func Lock

func Lock(plaintext, nonce, key []byte) (mac, ciphertext []byte)

Lock is authenticated encryption using XChacha20 & Poly1305. Lock takes plaintext, [24]nonce and [32]key, returns the [16]mac and the ciphertext.

Example
key := []byte("Gophers are such a fun thing. Hehehehehe")
// Record of nonce generated by crypto/rand.Read()
nonce := []byte{107, 131, 11, 111, 188, 30, 66, 190, 31, 157, 156, 136, 226, 107, 32, 114, 231, 159, 140, 245, 164, 170, 242, 191}
plain := []byte("Hello this is Gopher army, we are gonna rule this world ")
mac, cipher := Lock(plain, nonce, key)
fmt.Println("MAC", mac, "Cipher", cipher)
Output:

MAC [209 44 35 111 91 58 6 70 116 153 198 210 165 125 248 87] Cipher [62 210 135 119 146 59 215 233 197 251 31 141 50 104 181 169 2 21 19 227 29 85 158 85 244 21 46 124 232 46 171 148 127 92 227 129 227 68 181 49 125 91 206 210 81 115 140 66 154 186 195 181 254 142 202 145]

func SignMessage

func SignMessage(message, secretKey []byte) (signature, publicKey []byte)

Sign signs a message with your secret key. The generated curve225519 public key along with the signature is returned.

Example
message := []byte("Hello this is Gopher army, we are gonna rule this world ")
secretKey := []byte("Gophers are such a fun thing. Hehehehehe")
signature, publicKey := SignMessage(message, secretKey)
// fmt.Println(base64.StdEncoding.EncodeToString(signature))
fmt.Println(signature, publicKey)
Output:

[64 83 187 13 39 201 125 215 140 212 100 97 25 240 239 101 170 124 81 142 173 70 89 232 17 124 95 189 39 182 66 111 153 207 243 14 148 184 184 56 135 72 37 141 143 144 199 167 211 78 36 123 9 142 40 236 178 110 81 169 156 117 202 5] [224 191 49 187 175 40 57 91 163 14 204 126 226 196 209 163 20 55 100 80 214 24 49 221 59 62 177 82 191 215 161 202]

func Unlock

func Unlock(ciphertext, nonce, key, mac []byte) (plaintext []byte)

Unlock decrypts the ciphertext from Lock(). It first checks the integrity using mac, then uses the same [24]nonce and [32]key to decrypt the cipher and returns the plaintext in bytes.

Example
key := []byte("Gophers are such a fun thing. Hehehehehe")
nonce := []byte{107, 131, 11, 111, 188, 30, 66, 190, 31, 157, 156, 136, 226, 107, 32, 114, 231, 159, 140, 245, 164, 170, 242, 191}
mac := []byte{209, 44, 35, 111, 91, 58, 6, 70, 116, 153, 198, 210, 165, 125, 248, 87}
cipher := []byte{62, 210, 135, 119, 146, 59, 215, 233, 197, 251, 31, 141, 50, 104, 181, 169, 2, 21, 19, 227, 29, 85, 158, 85, 244, 21, 46, 124, 232, 46, 171, 148, 127, 92, 227, 129, 227, 68, 181, 49, 125, 91, 206, 210, 81, 115, 140, 66, 154, 186, 195, 181, 254, 142, 202, 145}
plain := Unlock(cipher, nonce, key, mac)
fmt.Println(string(plain))
Output:

Hello this is Gopher army, we are gonna rule this world

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL