U R]@@sdZddlmZmZddlmZddlZddlmZddlm Z ddlm Z ddlm Z dd l m Z dd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZGddde ZddZGdddeZGdddeZeddZeddZd d!Z d"d#Z!d$d%Z"d&d'Z#d(Z$d)Z%d*Z&e'e$gZ(e'e%gZ)e j*d+e j+d,e j,d-e j-d.e j.d/e j/d0e j0d1e j1d2e j2d3e j3d4e j4d5e j5d6e j6d7i Z7d8Z8e'd9gZ9Gd:d;d;Z:Gdd?d?e;ZdDdEZ?dFdGZ@dHdIZAe j*e=e j+ee j,ee j-ee j.ee j/ee j0e?e j1e>e j2ee j3ei ZBdJdKZCdLdMZDdNdOZEdPdQZFdS)RzAdds an `ion_hash()` method to all simpleion value classes, and provides readers/writers that hash Ion values according to the Ion Hash Specification.)ABCabstractmethod) cmp_to_keyN) DataEvent)IonEvent) IonEventType)IonType)Enum) coroutine) NEXT_EVENT) SKIP_EVENT)_serialize_blob)_serialize_bool)_serialize_clob)_serialize_decimal)_serialize_float)_serialize_int)_serialize_timestampc@seZdZdZdZdS) HashEventzEvents that may be pushed into a hash_reader or hash_writer coroutine, in addition to those allowed by the wrapped reader/writer. Attributes: DIGEST: produces `bytes` that represents the hash of the Ion values read/written rN)__name__ __module__ __qualname____doc__DIGESTrr5/tmp/pip-target-hkr6o2s8/lib/python/ionhash/hasher.pyr&srcsfdd}|S)z,A hash function provider based on `hashlib`.cstSN) _HashlibHashr algorithmrr_f2sz*hashlib_hash_function_provider.._fr)rr rrrhashlib_hash_function_provider0s r!c@s(eZdZdZeddZeddZdS) IonHasherzAbstract class declaring the hashing methods that must be implemented in order to support a hash function for use by `hash_reader` or `hash_writer`.cCsdS)z4Updates the hash function with the specified _bytes.Nrself_bytesrrrupdate:szIonHasher.updatecCsdS)zvReturns a digest of the accumulated bytes passed to `update`, and resets the `IonHasher` to its initial state.Nrr$rrrdigest?szIonHasher.digestN)rrrrrr&r(rrrrr"7s  r"c@s(eZdZdZddZddZddZdS) rzZImplements the expected hash function methods for the specified algorithm using `hashlib`.cCs||_t|j|_dSr) _algorithmhashlibnew_hasher)r$rrrr__init__Hsz_HashlibHash.__init__cCs|j|dSr)r,r&r#rrrr&Lsz_HashlibHash.updatecCs|j}t|j|_|Sr)r,r(r*r+r)r$r(rrrr(Os z_HashlibHash.digestN)rrrrr-r&r(rrrrrFsrcCs tt||S)aProvides a coroutine that wraps an ion-python reader and adds Ion Hash functionality. The given coroutine yields `bytes` when given ``HashEvent.DIGEST``. Otherwise, the couroutine's behavior matches that of the wrapped reader. Notes: The coroutine translates any amazon.ion.reader.SKIP_EVENTs into a series of amazon.ion.reader.NEXT_EVENTs in order to ensure that the hash correctly includes any subsequent or nested values. Args: reader(couroutine): An ion-python reader coroutine. hash_function_provider(function): A function that returns a new ``IonHasher`` instance when called. Note that multiple ``IonHasher`` instances may be required to hash a single value (depending on the type of the Ion value). Yields: bytes: The result of hashing. other values: As defined by the provided reader coroutine. )r,_hash_reader_handler)readerhash_function_providerrrr hash_readerUsr2cCs tt||S)aProvides a coroutine that wraps an ion-python writer and adds Ion Hash functionality. The given coroutine yields `bytes` when given ``HashEvent.DIGEST``. Otherwise, the couroutine's behavior matches that of the wrapped writer. Args: writer(coroutine): An ion-python writer coroutine. hash_function_provider(function): A function that returns a new ``IonHasher`` instance when called. Note that multiple ``IonHasher`` instances may be required to hash a single value (depending on the type of the Ion value). Yields: bytes: The result of hashing. other values: As defined by the provided writer coroutine. )r,_hash_writer_handler)writerr1rrr hash_writertsr5ccsVt|}d}|V}t|tr2|tjkrP|V}q|||||}|dkrJqR|V}qdS)z_Provides a coroutine that wraps an ion-python reader or writer and adds Ion Hash functionality.N)_Hasher isinstancerrr()handlerZdelegater1hasheroutputinputrrrr,s   r,cCst|trp|tkrf|j}|jtjkr.|jd}|t}|jtj krp|j|krpt |||t}q8n ||}t|t rt |||S)z*Handles input to a reader-based coroutine.) r7rr depth event_typerCONTAINER_STARTsendr STREAM_END _hash_eventr)r;r:r9r0Z target_depthrrrr/s        r/cCs"t|tr||}t|||S)z*Handles input to a writer-based coroutine.)r7rr@rB)r;r:r9r4rrrr3s   r3cCsH|jtjkr||n,|jtjkr.|n|jtjk rD||dS)z2Maps an IonEvent to the appropriate hasher method.N)r>rr?step_inZ CONTAINER_ENDstep_outrAscalar)r9eventrrrrBs      rB   @P`pqc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)r6zPrimary driver of the Ion hash algorithm. This class maintains a stack of serializers corresponding to the nesting of Ion data being hashed. cCs$||_t|d|_|jg|_dSNr)_hash_function_provider _Serializer_current_hasher _hasher_stack)r$r1rrrr-sz_Hasher.__init__cCs|j|dSr)r\rEr$ ion_eventrrrrEsz_Hasher.scalarcCsl|jj}t|jtr|}|jtjkr>t|||j|_nt |||_|j |j|j |dSr) r\ hash_functionr7_StructSerializerrZion_typerSTRUCT_depthr[r]appendrC)r$r_ZhfrrrrCs  z_Hasher.step_incCsX|dkrtd|j|j}|jd|_t|jtrT|}|j |dS)Nrz"Hasher cannot step_out any further) rd Exceptionr\rDr]popr7rar(append_field_hash)r$Z popped_hasherr(rrrrDs     z_Hasher.step_outcCs|dkrtd|jS)Nrz?A digest may only be provided at the same depth hashing started)rdrgr\r(r'rrrr(s z_Hasher.digestcCst|jdSNr<)lenr]r'rrrrdsz_Hasher._depthN) rrrrr-rErCrDr(rdrrrrr6s  r6c@steZdZdZddZddZdddZdd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd S)r[zs                        /F