// prepare key for low-level poly1305.Sum(): r||n func poly1305PrepareKey(nonce []byte, key *MACKey) [32]byte { var k [32]byte maskKey(key) cipher, err := aes.NewCipher(key.K[:]) if err != nil { panic(err) } cipher.Encrypt(k[16:], nonce[:]) copy(k[:16], key.R[:]) return k }
// stringhash computes generic string hash. Uses k as the key for AES // cipher. func stringhash(s string, k []byte) []byte { a := bytes_to_a32(paddnull([]byte(s), 4)) h := []uint32{0, 0, 0, 0} for i, v := range a { h[i&3] ^= v } hb := a32_to_bytes(h) cipher, _ := aes.NewCipher(k) for i := 16384; i > 0; i-- { cipher.Encrypt(hb, hb) } ha := bytes_to_a32(paddnull(hb, 4)) return a32_to_base64([]uint32{ha[0], ha[2]}) }
func Encrypt(text, key []byte) ([]byte, error) { // Create cipher and pad text cipher, padded, err := cipherAndPaddedText(text, key) if err != nil { return nil, err } keysize := len(key) // Encrypt each block for i := 0; i < len(padded)/keysize; i += keysize { cipher.Encrypt(padded[i:i+keysize], padded[i:i+keysize]) } return padded[:len(text)], nil }