4:30
     ˆ¾Âƒh¹ÁÀ      4:30
  "   	ˆl–ß=¿J*e¶¥8 q·®å1hßÃƒh¹ÂÁ      	4:29
  #   ˆl–äY>”*i?u ¸ ¸ \”Å£Ä„/²ÏÃÂ 6m    """passlib.handlers.sun_md5_crypt - Sun's Md5 Crypt, used on Solaris

.. warning::

    This implementation may not reproduce
    the original Solaris behavior in some border cases.
    See documentation for details.
"""

#=============================================================================
# imports
#=============================================================================
# core
from hashlib import md5
import re
import logging; log = logging.getLogger(__name__)
from warnings import warn
# site
# pkg
from passlib.utils import to_unicode
from passlib.utils.binary import h64
from passlib.utils.compat import byte_elem_value, irange, u, \
                                 uascii_to_str, unicode, str_to_bascii
import passlib.utils.handlers as uh
# local
__all__ = [
    "sun_md5_crypt",
]

#=============================================================================
# backend
#=============================================================================
# constant data used by alg - Hamlet act 3 scene 1 + null char
# exact bytes as in http://www.ibiblio.org/pub/docs/books/gutenberg/etext98/2ws2610.txt
# from Project Gutenberg.

MAGIC_HAMLET = (
    b"To be, or not to be,--that is the question:--\n"
    b"Whether 'tis nobler in the mind to suffer\n"
    b"The slings and arrows of outrageous fortune\n"
    b"Or to take arms against a sea of troubles,\n"
    b"And by opposing end them?--To die,--to sleep,--\n"
    b"No more; and by a sleep to say we end\n"
    b"The heartache, and the thousand natural shocks\n"
    b"That flesh is heir to,--'tis a consummation\n"
    b"Devoutly to be wish'd. To die,--to sleep;--\n"
    b"To sleep! perchance to dream:--ay, there's the rub;\n"
    b"For in that sleep of death what dreams may come,\n"
    b"When we have shuffled off this mortal coil,\n"
    b"Must give us pause: there's the respect\n"
    b"That makes calamity of so long life;\n"
    b"For who would bear the whips and scorns of time,\n"
    b"The oppressor's wrong, the proud man's contumely,\n"
    b"The pangs of despis'd love, the law's delay,\n"
    b"The insolence of office, and the spurns\n"
    b"That patient merit of the unworthy takes,\n"
    b"When he himself might his quietus make\n"
    b"With a bare bodkin? who would these fardels bear,\n"
    b"To grunt and sweat under a weary life,\n"
    b"But that the dread of something after death,--\n"
    b"The undiscover'd country, from whose bourn\n"
    b"No traveller returns,--puzzles the will,\n"
    b"And makes us rather bear those ills we have\n"
    b"Than fly to others that we know not of?\n"
    b"Thus conscience does make cowards of us all;\n"
    b"And thus the native hue of resolution\n"
    b"Is sicklied o'er with the pale cast of thought;\n"
    b"And enterprises of great pith and moment,\n"
    b"With this regard, their currents turn awry,\n"
    b"And lose the name of action.--Soft you now!\n"
    b"The fair Ophelia!--Nymph, in thy orisons\n"
    b"Be all my sins remember'd.\n\x00" #<- apparently null at end of C string is included (test vector won't pass otherwise)
)

# NOTE: these sequences are pre-calculated iteration ranges used by X & Y loops w/in rounds function below
xr = irange(7)
_XY_ROUNDS = [
    tuple((i,i,i+3) for i in xr), # xrounds 0
    tuple((i,i+1,i+4) for i in xr), # xrounds 1
    tuple((i,i+8,(i+11)&15) for i in xr), # yrounds 0
    tuple((i,(i+9)&15, (i+12)&15) for i in xr), # yrounds 1
]
del xr

def raw_sun_md5_crypt(secret, rounds, salt):
    """given secret & salt, return encoded sun-md5-crypt checksum"""
    global MAGIC_HAMLET
    assert isinstance(secret, bytes)
    assert isinstance(salt, bytes)

    # validate rounds
    if rounds <= 0:
        rounds = 0
    real_rounds = 4096 + rounds
    # NOTE: spec seems to imply max 'rounds' is 2**32-1

    # generate initial digest to start off round 0.
    # NOTE: algorithm 'salt' includes full config string w/ trailing "$"
    result 