Example #1
0
func (self *CipherCtx) EncryptInit(cipher *Cipher, key []byte, iv []byte) error {
	if cipher == nil {
		return errors.New("Cipher is required")
	}
	key_p := (*C.uchar)(unsafe.Pointer(C.CString(string(key))))
	defer C.free(unsafe.Pointer(key_p))
	iv_p := (*C.uchar)(unsafe.Pointer(C.CString(string(iv))))
	defer C.free(unsafe.Pointer(iv_p))
	ret := int(C.EVP_EncryptInit(
		self.evp_cipher_ctx, cipher.evp_cipher, key_p, iv_p))
	if ret == 1 {
		return nil
	}
	return errors.New("failure")
}
Example #2
0
func (ctx *CipherCtx) EncryptInit(cipher *Cipher, key []byte, iv []byte) error {
	if cipher == nil {
		return ErrCipherRequired
	}
	key_p := (*C.uchar)(unsafe.Pointer(C.CString(string(key))))
	defer C.free(unsafe.Pointer(key_p))
	iv_p := (*C.uchar)(unsafe.Pointer(C.CString(string(iv))))
	defer C.free(unsafe.Pointer(iv_p))
	ret := int(C.EVP_EncryptInit(
		ctx.evp_cipher_ctx, cipher.evp_cipher, key_p, iv_p))
	if ret != 1 {
		// try to return the openssl error first, if present
		if err := sslerr.Error(); err != nil {
			return err
		} else {
			return ErrFailure
		}
	}
	return nil
}