Documentation ¶
Overview ¶
Package cry provides utilities for cryptography. This library has not been vetted and people are discouraged from using it. Instead, use the cryptography facilities in the Go standard library and/or golang.org/x/crypto
Index ¶
- func Eql(password, hash string) error
- func Hash(password string) (string, error)
- type Enc
- func (e Enc) Decrypt(encryptedMsg []byte) (decryptedMsg []byte, err error)
- func (e Enc) DecryptDecode(encryptedEncodedMsg string) (plainTextMsg string, err error)
- func (e Enc) Encrypt(plainTextMsg string) (encryptedMsg []byte)
- func (e Enc) EncryptEncode(plainTextMsg string) (encryptedEncodedMsg string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Eql ¶
Eql performs a constant-time comparison between the password and the hash. The hash ought to have been produced by Hash
func Hash ¶
Hash returns the scrypt hash of the password. It is safe to persist the result in your database instead of storing the actual password.
Example ¶
package main import ( "fmt" "github.com/komuw/ong/cry" ) func main() { password := "my NSA-hard password" // it is okay to save hashedPasswd to the database, as an example. hashedPasswd, err := cry.Hash(password) if err != nil { panic(err) } err = cry.Eql(password, hashedPasswd) if err != nil { panic(err) } fmt.Println(hashedPasswd) }
Output:
Types ¶
type Enc ¶
type Enc struct {
// contains filtered or unexported fields
}
Enc is an AEAD cipher mode providing authenticated encryption with associated data, ie cipher.AEAD
Use New to get a valid Enc.
func New ¶
New returns a cipher.AEAD
It panics on error.
It uses scrypt to derive the final key that will be used for encryption.
func (Enc) Decrypt ¶
Decrypt authenticates and un-encrypts the encryptedMsg using XChaCha20-Poly1305 and returns decrypted bytes.
func (Enc) DecryptDecode ¶
DecryptDecode takes an encryptedEncodedMsg that was generated using Enc.EncryptEncode and returns the original un-encrypted string.
func (Enc) Encrypt ¶
Encrypt, encrypts and authenticates(tamper-proofs) the plainTextMsg using XChaCha20-Poly1305 and returns encrypted bytes.
Example ¶
package main import ( "github.com/komuw/ong/cry" ) func main() { key := "hard-passwd" e := cry.New(key) plainTextMsg := "Muziki asili yake - Remmy Ongala." // English: `What is the origin of music by Remmy Ongala` encryptedMsg := e.Encrypt(plainTextMsg) _ = encryptedMsg }
Output:
func (Enc) EncryptEncode ¶
EncryptEncode is like Enc.Encrypt except that it returns a string that is encoded using base64.RawURLEncoding
Example ¶
package main import ( "fmt" "github.com/komuw/ong/cry" ) func main() { key := "hard-passwd" e := cry.New(key) originalPlainTextMsg := "three little birds." encryptedEncodedMsg := e.EncryptEncode(originalPlainTextMsg) resultantPlainTextMsg, err := e.DecryptDecode(encryptedEncodedMsg) if err != nil { panic(err) } if resultantPlainTextMsg != originalPlainTextMsg { panic("something went wrong") } fmt.Println(resultantPlainTextMsg) }
Output: three little birds.