Documentation ¶
Overview ¶
Package symcrypto is a URL safe encryption/decryption library.
It uses golang.org/x/crypto/nacl/secretbox under the hood which is suitable for encrypting small messages. "encoding/base64" is used to make the encrypted token URL safe.
Example ¶
package main import ( "fmt" "github.com/sudo-suhas/symcrypto" ) func handleError(err error) { if err != nil { panic(err) } } func main() { // Load your secret key from a safe place and use it to create an instance of // `Crypter`. (Obviously don't use this example key for anything real.) If you want // to convert a passphrase to a key, use a suitable package like bcrypt or scrypt. crypto, err := symcrypto.New("6368616e676520746869732070617373") handleError(err) // This returns the nonce appended with the encrypted string for "hello world". encrypted, err := crypto.Encrypt("hello world") handleError(err) // Example encrypted message - "2eUERfII6K7djr-GCR2qwSf8LJ-7ZoOmdlT54HPkhw297ML46M6VvlpvW2LrA_Ewge-2" // Because of unique nonce, encrypted message will vary for the same input. // The encrypted string can be decrypted only by using the `Crypter` instance which // was used to encrypt it. decrypted, err := crypto.Decrypt(encrypted) handleError(err) fmt.Printf("Decrypted message - %q\n", decrypted) }
Output: Decrypted message - "hello world"
Index ¶
Examples ¶
Constants ¶
const SecretKeyLen = 32
SecretKeyLen is the minimum length of secret key required for creating an instance of Crypter. NaCl expects the secret key to be 32 characters long.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Crypter ¶
type Crypter interface { // Encrypt encrypts the given message using the configured secret key and returns the // encrypted string. The encrypted string is encoded using base64 so that it can be // used in the URL. Encrypt(string) (string, error) // Decrypt decrypts and returns the given token using the configured secret key. The // encrypted string is expected to be base64 encoded. Decrypt(string) (string, error) }
Crypter does encryption and decription using the given secret key. The encrypted string is URL safe via base64 encoding.
func New ¶
New creates an instance of Crypter which can be used for encrypting/decrypting with the same secret key.
It is recommedded to load your secret key from a safe place and use it for instantiating a Crypter instance. If you want to convert a passphrase to a key, use a suitable package like bcrypt or scrypt. The secret key must be at least 32 chars long. If the length exceeds 32 chars, the mid 32 chars will be used as the secret key.