emergency_contact_info_data.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. return True
  28. if row.sign == '':
  29. return True
  30. # 关键字段合并字符串
  31. sign_data = get_sign_str(row)
  32. # print('sys_user sign_data:', sign_data)
  33. # 原HMACSM3数值
  34. sign_hmac = row.sign
  35. # print('sign_hmac:', sign_hmac)
  36. return mpfun.hmac_verify(sign_data, sign_hmac)
  37. # 生成待签名的字符串
  38. def get_sign_str(row: EmergencyContactInfo) -> str:
  39. unit_id = str(row.unit_id) # 单位ID
  40. unit_name = mpfun.base64_data(row.unit_name) # 单位名称
  41. contact_name = mpfun.base64_data(row.contact_name) # 联系人
  42. position = mpfun.base64_data(row.position) # 职务
  43. yue_gov_ease_phone = row.yue_gov_ease_phone # 粤政易手机号码
  44. del_flag = row.del_flag # 是否已删除
  45. # 关键字段合并字符串
  46. sign_data = ",".join([unit_id, unit_name, contact_name, position, yue_gov_ease_phone, del_flag])
  47. return sign_data
  48. # 生成HAMC签名值
  49. def get_sign_hmac(row: EmergencyContactInfo) -> str:
  50. sign_data = get_sign_str(row)
  51. return mpfun.sign_data(sign_data)
  52. # 对所有数据进行签名
  53. def sign_table():
  54. print('sign_emergency_contact_info table =====>>>')
  55. with get_local_db() as db:
  56. rows = db.query(EmergencyContactInfo).filter(EmergencyContactInfo.sign == '').all()
  57. for row in rows:
  58. sign_row(db, row)