encryHlsVodUrl.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/6/8 15:22
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: encryHlsVodUrl.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. # coding=utf-8
  15. # + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +
  16. # ┏┓   ┏┓+ +
  17. #    ┏┛┻━━━┛┻┓ + +
  18. #    ┃       ┃  
  19. #    ┃   ━   ┃ ++ + + +
  20. #    ████━████ ┃+
  21. #    ┃       ┃ +
  22. #    ┃   ┻   ┃
  23. #    ┃       ┃ + +
  24. #    ┗━┓   ┏━┛
  25. #      ┃   ┃           
  26. #      ┃   ┃ + + + +
  27. #      ┃   ┃    Codes are far away from bugs with the animal protecting   
  28. #      ┃   ┃ +     神兽保佑,代码无bug  
  29. #      ┃   ┃
  30. #      ┃   ┃  +         
  31. #      ┃    ┗━━━┓ + +
  32. #      ┃        ┣┓
  33. #      ┃        ┏┛
  34. #      ┗┓┓┏━┳┓┏┛ + + + +
  35. #       ┃┫┫ ┃┫┫
  36. #       ┗┻┛ ┗┻┛+ + + +
  37. # + + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + ++ + + +"""
  38. """
  39. """
  40. import base64
  41. import hashlib
  42. import time
  43. def md5(text, isBackByte=False):
  44. """md5加密函数"""
  45. md5 = hashlib.md5()
  46. if isinstance(text, bytes):
  47. md5.update(text)
  48. else:
  49. md5.update(text.encode('utf-8'))
  50. if isBackByte:
  51. # 返回二进制的加密结果
  52. return md5.digest()
  53. # 返回十六进制的机密结果
  54. return md5.hexdigest()
  55. def base64_encode(text, isBytes=False):
  56. """进行base64编码处理"""
  57. if isBytes:
  58. return base64.b64encode(text)
  59. return base64.b64encode(bytes(text, encoding="utf-8"))
  60. def get_timestamp10():
  61. """获取当前时间长度为10位长度的时间戳"""
  62. return int(time.time())
  63. def getSignUrl(path):
  64. secret = 'ansjer'; # 密钥--对应#st的哈希格式为 secret+url+e,e为时间戳单位s,url为请求地址 secure_link_md5 xiaozhong.com$uri$arg_e;
  65. # 下载到期时间,time是当前时间,300表示300秒,也就是说从现在到300秒之内文件不过期
  66. expire = get_timestamp10() + 1800;
  67. res = md5(str(secret) + str(path) + str(expire), True)
  68. md5str = str(base64_encode(res, True))
  69. md5st = md5str.replace('b\'', '').replace('\'', '').replace('+', '-').replace('/', '_').replace('=', '')
  70. url = 'http://52.8.197.107'+path+'.m3u8?' + 'st=' + str(md5st) + '&e=' + str(expire)
  71. return url
  72. # print('生成代相关认证签名的地址:','http://192.168.136.45:8384/hls/5553.mp4/index.m3u8?' + 'st=' + str(md5) + '&e=' + str(expire))
  73. # print('生成代相关认证签名的地址:','http://52.8.197.107/444.mp4.m3u8?' + 'st=' + str(md5) + '&e=' + str(expire))
  74. # print('生成代相关认证签名的地址:','http://d3puj5e5oiunfy.cloudfront.net/444.mp4.m3u8?' + 'st=' + str(md5) + '&e=' + str(expire))