def decrypt(enc, key, hmac_key=None, key_is_hash=False, b64=True, bits=256):
    """
    Decrypt encoded data with AES-CBC

    Args:
        enc: data to decrypt
        key: decryption key
        key_is_hash: consider decryption key is sha256 hash
        hmac_key: HMAC key (optional), True or custom key
        b64: decode data from base64 (default: True)
        bits: key size (128, 192 or 256, default is 256)
    Raises:
        ValueError: if HMAC auth failed

    note: if hmac_key is True and key is hash, sha512 sum is required
    """
    from Crypto.Cipher import AES
    import hashlib
    import hmac
    if b64:
        import base64
        enc = base64.b64decode(enc)
    if hmac_key is True:
        h = key if key_is_hash else hashlib.sha512(key.encode() if
            isinstance(key, str) else key).digest()
        keyhash = h[:bits // 8]
        hmac_key = h[:-32]
    else:
        keyhash = key if key_is_hash else hashlib.sha256(key.encode() if
            isinstance(key, str) else key).digest()[:bits // 8]
    iv = enc[:16]
    if hmac_key:
        if hmac.new(hmac_key.encode() if isinstance(hmac_key, str) else
            hmac_key, enc[:-32], hashlib.sha256).digest() != enc[-32:]:
            raise ValueError('HMAC auth failed')
    cipher = AES.new(keyhash, AES.MODE_CBC, iv)
    data = cipher.decrypt(enc[16:-32 if hmac_key else None])
    return data[:-data[-1]]
