python ID混淆-短id

!/usr/bin/env python

-- coding: utf-8 --

########################################################################

Copyright (c) 2018 Baidu.com, Inc. All Rights Reserved

########################################################################
"""
File : 3des.py
"""
import sys
import json
import time
import os
import commands

安装相关依赖

try:
import hashids
except:
try:
install_cmd = 'python -m pip install hashids'
status, output = commands.getstatusoutput(install_cmd)
import hashids
except Exception as e:
print(e)
pass
try:
import murmurhash3
except:
try:
install_cmd = 'python -m pip install murmurhash3'
status, output = commands.getstatusoutput(install_cmd)
import murmurhash3
except Exception as e:
print(e)
pass

reload(sys)
sys.setdefaultencoding('utf8')
dpspath = '/home/work/search/dps'
if 'DPS_WORK_DIR' in os.environ:
dpspath = os.environ['DPS_WORK_DIR']
if dpspath[-4:] != '/dps':
dpspath = dpspath + '/dps'
os.environ['DPS_WORK_DIR'] = dpspath
sys.path.append(dpspath + '/')
sys.path.append('/home/work/search/dps/baselib/dps/')
from function import Function

class Confuse(Function):
"""
bos客户端使用
"""
def init(self):
"""
init
"""
def counfuse_id(self, id):
"""
id 混淆函数
"""
counfuse_id = str(id)

    # hashids 混淆id
    if is_number(counfuse_id):
        hasher = hashids.Hashids(min_length=6, salt="lvlin")
        ensecret = hasher.encode(1, 2, 3)
        counfuse_id = ensecret
    else:
        return counfuse_id + "---"
    

    return counfuse_id

def is_number(s):
"""
判断是否是数字
"""
try:
float(s)
return True
except ValueError:
pass

try:
    import unicodedata
    unicodedata.numeric(s)
    return True
except (TypeError, ValueError):
    pass

return False

if name == 'main':

a = Confuse()
res = a.counfuse_id("cc")
print("res=", res)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容