stringpasswordencrypt

package module
v0.0.0-...-cfc54b5 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2020 License: MIT Imports: 5 Imported by: 0

README

string-password-encrypt

A an example library for encrypting and decrypting a string using a password.

Was originally based heavily on "https://github.com/isfonzar/filecrypt", however after some reading, I moved to a scrypt implementation rather than pbkdf2

Caveat Emptor: Whilst this does seem to follow the golang crypto fundamentals as far as I can tell, it's making a lot of assumptions and using a lot of defaults. For example, it's using the N value of 32768 which is the recomendation for interactive logins as of 2017

Example

import "github.com/petems/string-password-encrypt"

encryptedValue := EncryptValue("secret", "password123")
decryptedValue := DecryptValue(encryptedValue, "password123")

The value encypted value will be binary, so won't be useful for storing in a config file:

import "github.com/petems/string-password-encrypt"

encryptedValue := EncryptValue("secret", "password123")
fmt.Print("Value: " + encryptedValue) // Value: f����V\�W(5`��{T�w_U�.k�?	)�:�fい��H��!�p����y}��|�n'���E�

So we've got two base64 encoding helper methods, so you can encode it to a Base64 URL Encoding for easy storage in a config file

import "github.com/petems/string-password-encrypt"

encryptedValue := EncryptValue("secret", "password123")

base64encrypted := Base64Encode([]byte(encryptedValue))

fmt.Print("Value: " + base64encrypted) // Value: qj_eW3AEIjcAjOkow0m6HjIFecjJQJ2l55ZL86eIu6SzG0CneqVYVA_RUWUufQbGwWdvDyJgDkOjEk5b2NpLwVLA

base64decrypted, err := Base64Decode(base64encrypted)

if err != nil {
	panic(err)
}

decryptedValue := DecryptValue([]byte(base64decrypted), "password123")

Documentation

Overview

Example

This is an example of encrypting and decrypting a value with a password

encryptedValue, _ := Encrypt("password123", "secret")
decryptedValue, _ := Decrypt("password123", encryptedValue)
fmt.Println(string(decryptedValue))
Output:

secret

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64Decode

func Base64Decode(data string) (string, error)

func Base64Encode

func Base64Encode(data string) string

func Decrypt

func Decrypt(password, value string) (string, error)

Decrypt decrypts given byte ciphertext using a given password

func Encrypt

func Encrypt(password, value string) (string, error)

Encrypt encrypts given byte data using a given password

Types

This section is empty.

Jump to

Keyboard shortcuts

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