emergency_contact_info_data.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from . import mpfun
  4. from models import *
  5. from sqlalchemy.orm import Session
  6. from database import get_local_db
  7. # 应急预案人员信息表
  8. # 加密和HMAC签名
  9. def sign_row(db: Session, row: EmergencyContactInfo) -> None:
  10. if row.sign != '':
  11. return
  12. unit_id = str(row.unit_id) # 单位ID
  13. unit_name = mpfun.base64_data(row.unit_name) # 单位名称
  14. contact_name = mpfun.base64_data(row.contact_name) # 联系人
  15. position = mpfun.base64_data(row.position) # 职务
  16. yue_gov_ease_phone = mpfun.enc_data(row.yue_gov_ease_phone) # 粤政易手机号码
  17. del_flag = row.del_flag # 是否已删除
  18. sign_data = ",".join([unit_id, unit_name, contact_name, position, yue_gov_ease_phone, del_flag])
  19. sign_hmac = mpfun.sign_data(sign_data)
  20. # print('sign_tbl_user sign_data:', sign_data)
  21. # print('sign_tbl_user sign_hmac:', sign_hmac)
  22. row.yue_gov_ease_phone = yue_gov_ease_phone
  23. row.sign = sign_hmac
  24. db.commit()
  25. # 比较字段合并字符串是否和MAC值匹配上,调用密码服务器[验证HMAC]接口
  26. def sign_valid_row(row: EmergencyContactInfo) -> bool:
  27. if row.sign == '':
  28. return True
  29. # 关键字段合并字符串
  30. sign_data = get_sign_str(row)
  31. # print('sys_user sign_data:', sign_data)
  32. # 原HMACSM3数值
  33. sign_hmac = row.sign
  34. # print('sign_hmac:', sign_hmac)
  35. return mpfun.hmac_verify(sign_data, sign_hmac)
  36. # 生成待签名的字符串
  37. def get_sign_str(row: EmergencyContactInfo) -> str:
  38. unit_id = str(row.unit_id) # 单位ID
  39. unit_name = mpfun.base64_data(row.unit_name) # 单位名称
  40. contact_name = mpfun.base64_data(row.contact_name) # 联系人
  41. position = mpfun.base64_data(row.position) # 职务
  42. yue_gov_ease_phone = row.yue_gov_ease_phone # 粤政易手机号码
  43. del_flag = row.del_flag # 是否已删除
  44. # 关键字段合并字符串
  45. sign_data = ",".join([unit_id, unit_name, contact_name, position, yue_gov_ease_phone, del_flag])
  46. return sign_data
  47. # 生成HAMC签名值
  48. def get_sign_hmac(row: EmergencyContactInfo) -> str:
  49. sign_data = get_sign_str(row)
  50. return mpfun.sign_data(sign_data)
  51. # 对所有数据进行签名
  52. def sign_table():
  53. print('sign_emergency_contact_info table =====>>>')
  54. with get_local_db() as db:
  55. rows = db.query(EmergencyContactInfo).filter(EmergencyContactInfo.sign == '').all()
  56. for row in rows:
  57. sign_row(db, row)