flask计算pin值

python3.8和python3.6生成PIN值算法不同!
python3.8和python3.6生成PIN值算法不同!
python3.8和python3.6生成PIN值算法不同!

直接看python3.8中flask中的生成源码,路径为

/usr/local/lib/python3.8/dist-packages/werkzeug/debug/__init__.py
...前面略 
# A week
PIN_TIME = 60 * 60 * 24 * 7
​
​
def hash_pin(pin: str) -> str:
    return hashlib.sha1(f"{pin} added salt".encode("utf-8", "replace")).hexdigest()[:12]
​
​
_machine_id: str | bytes | None = None
​
​
def get_machine_id() -> str | bytes | None:
    global _machine_id
​
    if _machine_id is not None:
        return _machine_id
​
    def _generate() -> str | bytes | None:
        linux = b""
​
        # machine-id is stable across boots, boot_id is not.
        for filename in "/etc/machine-id", "/proc/sys/kernel/random/boot_id":
            try:
                with open(filename, "rb") as f:
                    value = f.readline().strip()
            except OSError:
                continue
​
            if value:
                #这里进行了拼接,切记
                linux += value
                break
​
        # Containers share the same machine id, add some cgroup
        # information. This is used outside containers too but should be
        # relatively stable across boots.
        try:
           #这里读到值类似
#1:name=systemd:/docker/834bcd57137ff60db24c95b99329fc19422c65dbbcec978209ddba4549073ea4
            
            with open("/proc/self/cgroup", "rb") as f:
                linux += f.readline().strip().rpartition(b"/")[2]
        except OSError:
            pass
​
        #最终machine id
        #6a572e8e-05fd-4af4-883b-57e48b8110ce834bcd57137ff60db24c95b99329fc19422065dbbcecs78209ddba4549073ea4
        
        if linux:
            #print("mechine id is" + str(linux)),diy
            return linux
​
        # On OS X, use ioreg to get the computer's serial number.
        try:
            # subprocess may not be available, e.g. Google App Engine
            # https://github.com/pallets/werkzeug/issues/925
            from subprocess import PIPE
            from subprocess import Popen
​
            dump = Popen(
                ["ioreg", "-c", "IOPlatformExpertDevice", "-d", "2"], stdout=PIPE
            ).communicate()[0]
            match = re.search(b'"serial-number" = <([^>]+)', dump)
​
            if match is not None:
                return match.group(1)
        except (OSError, ImportError):
            pass
​
        # On Windows, use winreg to get the machine guid.
        if sys.platform == "win32":
            import winreg
​
            try:
                with winreg.OpenKey(
                    winreg.HKEY_LOCAL_MACHINE,
                    "SOFTWARE\\Microsoft\\Cryptography",
                    0,
                    winreg.KEY_READ | winreg.KEY_WOW64_64KEY,
                ) as rk:
                    guid: str | bytes
                    guid_type: int
                    guid, guid_type = winreg.QueryValueEx(rk, "MachineGuid")
​
                    if guid_type == winreg.REG_SZ:
                        return guid.encode()
​
                    return guid
            except OSError:
                pass
​
        return None
​
    _machine_id = _generate()
    return _machine_id
​
​
class _ConsoleFrame:
    """Helper class so that we can reuse the frame console code for the
    standalone console.
    """
​
    def __init__(self, namespace: dict[str, t.Any]):
        self.console = Console(namespace)
        self.id = 0
​
    def eval(self, code: str) -> t.Any:
        return self.console.eval(code)
​
​
def get_pin_and_cookie_name(
    app: WSGIApplication,
) -> tuple[str, str] | tuple[None, None]:
    """Given an application object this returns a semi-stable 9 digit pin
    code and a random key.  The hope is that this is stable between
    restarts to not make debugging particularly frustrating.  If the pin
    was forcefully disabled this returns `None`.
​
    Second item in the resulting tuple is the cookie name for remembering.
    """
    pin = os.environ.get("WERKZEUG_DEBUG_PIN")
    rv = None
    num = None
​
    # Pin was explicitly disabled
    if pin == "off":
        return None, None
​
    # Pin was provided explicitly
    if pin is not None and pin.replace("-", "").isdecimal():
        # If there are separators in the pin, return it directly
        if "-" in pin:
            rv = pin
        else:
            num = pin
​
    modname = getattr(app, "__module__", t.cast(object, app).__class__.__module__)
    username: 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值