encryDownLoad.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # coding=utf-8
  2. # + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +
  3. # ┏┓   ┏┓+ +
  4. #    ┏┛┻━━━┛┻┓ + +
  5. #    ┃       ┃  
  6. #    ┃   ━   ┃ ++ + + +
  7. #    ████━████ ┃+
  8. #    ┃       ┃ +
  9. #    ┃   ┻   ┃
  10. #    ┃       ┃ + +
  11. #    ┗━┓   ┏━┛
  12. #      ┃   ┃           
  13. #      ┃   ┃ + + + +
  14. #      ┃   ┃    Codes are far away from bugs with the animal protecting   
  15. #      ┃   ┃ +     神兽保佑,代码无bug  
  16. #      ┃   ┃
  17. #      ┃   ┃  +         
  18. #      ┃    ┗━━━┓ + +
  19. #      ┃        ┣┓
  20. #      ┃        ┏┛
  21. #      ┗┓┓┏━┳┓┏┛ + + + +
  22. #       ┃┫┫ ┃┫┫
  23. #       ┗┻┛ ┗┻┛+ + + +
  24. # + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +"""
  25. """
  26. """
  27. import base64
  28. import hashlib
  29. import time
  30. def md5(text, isBackByte=False):
  31. """md5加密函数"""
  32. md5 = hashlib.md5()
  33. if isinstance(text, bytes):
  34. md5.update(text)
  35. else:
  36. md5.update(text.encode('utf-8'))
  37. if isBackByte:
  38. # 返回二进制的加密结果
  39. return md5.digest()
  40. # 返回十六进制的机密结果
  41. return md5.hexdigest()
  42. def base64_encode(text, isBytes=False):
  43. """进行base64编码处理"""
  44. if isBytes:
  45. return base64.b64encode(text)
  46. return base64.b64encode(bytes(text, encoding="utf-8"))
  47. def get_timestamp10():
  48. """获取当前时间长度为10位长度的时间戳"""
  49. return int(time.time())
  50. secret = 'ansjer'; # 密钥--对应#st的哈希格式为 secret+url+e,e为时间戳单位s,url为请求地址 secure_link_md5 xiaozhong.com$uri$arg_e;
  51. # path = '/hls/5553.mp4/index.m3u8' # 下载文件
  52. # path = '/L59KVYDAEPHR1T6M111A_0/555666.mp4' # 下载文件
  53. path = '/5553.mp4' # 下载文件
  54. # path = '/444.mp4' # 下载文件
  55. # 下载到期时间,time是当前时间,300表示300秒,也就是说从现在到300秒之内文件不过期
  56. expire = get_timestamp10() + 3600;
  57. res = md5(str(secret) + str(path) + str(expire), True)
  58. md5 = str(base64_encode(res, True))
  59. md5 = md5.replace('b\'', '').replace('\'', '').replace('+', '-').replace('/', '_').replace('=', '')
  60. # print('生成代相关认证签名的地址:','http://52.8.197.107/444.mp4.m3u8?' + 'st=' + str(md5) + '&e=' + str(expire))
  61. print('io:','http://52.8.197.107/5553.mp4.m3u8?' + 'st=' + str(md5) + '&e=' + str(expire))