def add_feedback(self, user_id, feedback_type, content): data = dict() data["user_id"] = user_id data["feedback_type"] = feedback_type data["content"] = OHHOOperation.to_bytes(content) success = self.feedback.add(data) if success: result = Result.result_success() else: result = Result.result_failed() return result
def add_report(self, user_id, reported_user_id, report_type, content): data = dict() data["user_id"] = user_id data["reported_user_id"] = reported_user_id data["report_type"] = report_type data["content"] = OHHOOperation.to_bytes(content) success = self.report.add(data) if success: result = Result.result_success() else: result = Result.result_failed() return result
def encrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 加密文本必须是bytes(utf8或ascii),而不能是unicode(str) text = OHHOOperation.to_bytes(text) # 这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.目前AES-128足够用 length = 16 count = len(text) add = length - (count % length) text = text + (b'\0' * add) self.ciphertext = cryptor.encrypt(text) # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 # 所以这里统一把加密后的字符串转化为16进制字符串 # 以unicode存储文件信息 return OHHOOperation.to_str(b2a_hex(self.ciphertext))
def sha1(bytes_or_str): unicode_string = OHHOOperation.to_bytes(bytes_or_str) return hashlib.sha1(unicode_string).hexdigest()
def decrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 解密文本必须是必须是bytes(utf8或ascii),而不能是unicode(str) utf8_text = OHHOOperation.to_bytes(text) plain_text = cryptor.decrypt(a2b_hex(utf8_text)) return OHHOOperation.to_str(plain_text.rstrip(b'\0'))
def __init__(self, key): self.key = OHHOOperation.to_bytes(key) self.mode = AES.MODE_CBC
text = text + (b'\0' * add) self.ciphertext = cryptor.encrypt(text) # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题 # 所以这里统一把加密后的字符串转化为16进制字符串 # 以unicode存储文件信息 return OHHOOperation.to_str(b2a_hex(self.ciphertext)) # 解密后,去掉补足的空格用strip() 去掉 def decrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) # 解密文本必须是必须是bytes(utf8或ascii),而不能是unicode(str) utf8_text = OHHOOperation.to_bytes(text) plain_text = cryptor.decrypt(a2b_hex(utf8_text)) return OHHOOperation.to_str(plain_text.rstrip(b'\0')) if __name__ == '__main__': pc = OHHOEncryption(OHHOOperation.to_bytes('keyskeyskeyskeys')) # 初始化密钥 e = pc.encrypt("00000") print(len(e)) d = pc.decrypt(e) print(d) from Tools.ohho_random import OHHORandom test = OHHORandom.get_nonce(15) test_password = OHHOEncryption(OHHOOperation.to_str("ztr7vnwg4jiaeqh8")) print(test_password.encrypt(test)) b = "e332f36055acd3e0272ab1b5a58b14fb" print(len(b))
def get_user_extension(self, obj): result = dict() domain_id = self.get_user_extension_work_domain_id(obj) if domain_id: result[USER_EXTENSION_WORK_DOMAIN_ID] = domain_id smoke_id = self.get_user_extension_smoke_id(obj) if smoke_id: result[USER_EXTENSION_SMOKE_ID] = smoke_id drink_id = self.get_user_extension_drink_id(obj) if drink_id: result[USER_EXTENSION_DRINK_ID] = drink_id profession_id = self.get_user_extension_profession_id(obj) if profession_id: result[USER_EXTENSION_PROFESSION_ID] = profession_id marriage = self.get_user_extension_marriage(obj) if marriage or marriage == 0: result[USER_EXTENSION_MARRIAGE] = marriage industry_id = self.get_user_extension_industry_id(obj) if industry_id: result[USER_EXTENSION_INDUSTRY_ID] = industry_id hometown_area = self.get_user_extension_hometown_area(obj) if hometown_area: result[USER_EXTENSION_HOMETOWN_AREA] = hometown_area current_area = self.get_user_extension_current_area(obj) if current_area: result[USER_EXTENSION_CURRENT_AREA] = current_area body_type_id = self.get_user_extension_body_type_id(obj) if body_type_id: result[USER_EXTENSION_BODY_TYPE_ID] = body_type_id weight = self.get_user_extension_weight(obj) if weight: result[USER_EXTENSION_WEIGHT] = weight height = self.get_user_extension_height(obj) if height: result[USER_EXTENSION_HEIGHT] = height email = self.get_user_extension_email(obj) if email: result[USER_EXTENSION_EMAIL] = email nickname = OHHOOperation.to_bytes( self.get_user_extension_nickname(obj)) if nickname: result[USER_EXTENSION_NICK_NAME] = nickname sex = self.get_user_extension_sex(obj) if sex or sex == 0: result[USER_EXTENSION_SEX] = sex birthday = self.get_user_extension_birthday(obj) if birthday: result[USER_EXTENSION_BIRTHDAY] = birthday certification = self.get_user_extension_certification(obj) if certification is not None: result[USER_EXTENSION_CERTIFICATION] = certification # real_name = OHHOOperation.to_bytes(self.get_user_extension_real_name(obj)) # if real_name: # result[USER_EXTENSION_REAL_NAME] = real_name # # identity_card = self.get_user_extension_identity_card(obj) # if identity_card: # result[USER_EXTENSION_IDENTITY_CARD] = identity_card return result