You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.7 KiB

  1. #!/usr/bin/env python
  2. # Generate youtube signature algorithm from test cases
  3. import sys
  4. tests = [
  5. # 92 - vflQw-fB4 2013/07/17
  6. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
  7. "mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
  8. # 90
  9. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
  10. "mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
  11. # 89
  12. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'",
  13. "/?;:|}<[{=+-_)(*&^%$#@!MqBVCXZASDFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuyt"),
  14. # 88 - vflapUV9V 2013/08/28
  15. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
  16. "ioplkjhgfdsazxcvbnm12<4567890QWERTYUIOZLKJHGFDSAeXCVBNM!@#$%^&*()_-+={[]}|:;?/>.3"),
  17. # 87
  18. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
  19. "uioplkjhgfdsazxcvbnm1t34567890QWE2TYUIOPLKJHGFDSAZXCVeNM!@#$^&*()_-+={[]}|:;?/>.<"),
  20. # 86 - vfluy6kdb 2013/09/06
  21. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  22. "yuioplkjhgfdsazxcvbnm12345678q0QWrRTYUIOELKJHGFD-AZXCVBNM!@#$%^&*()_<+={[|};?/>.S"),
  23. # 85 - vflkuzxcs 2013/09/11
  24. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
  25. "T>/?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOvUY.REWQ0987654321mnbqcxzasdfghjklpoiuytr"),
  26. # 84 - vflg0g8PQ 2013/08/29 (sporadic)
  27. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
  28. ">?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWq0987654321mnbvcxzasdfghjklpoiuytr"),
  29. # 83
  30. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
  31. ".>/?;}[{=+_)(*&^%<#!MNBVCXZASPFGHJKLwOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytreq"),
  32. # 82 - vflGNjMhJ 2013/09/12
  33. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
  34. ".>/?;}[<=+-(*&^%$#@!MNBVCXeASDFGHKLPOqUYTREWQ0987654321mnbvcxzasdfghjklpoiuytrIwZ"),
  35. # 81 - vflLC8JvQ 2013/07/25
  36. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
  37. "C>/?;}[{=+-(*&^%$#@!MNBVYXZASDFGHKLPOIU.TREWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  38. # 80 - vflZK4ZYR 2013/08/23 (sporadic)
  39. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>",
  40. "wertyuioplkjhgfdsaqxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&z(-+={[};?/>"),
  41. # 79 - vflLC8JvQ 2013/07/25 (sporadic)
  42. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/",
  43. "Z?;}[{=+-(*&^%$#@!MNBVCXRASDFGHKLPOIUYT/EWQ0q87659321mnbvcxzasdfghjkl4oiuytrewp"),
  44. ]
  45. tests_age_gate = [
  46. # 86 - vflqinMWD
  47. ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
  48. "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
  49. ]
  50. def find_matching(wrong, right):
  51. idxs = [wrong.index(c) for c in right]
  52. return compress(idxs)
  53. return ('s[%d]' % i for i in idxs)
  54. def compress(idxs):
  55. def _genslice(start, end, step):
  56. starts = '' if start == 0 else str(start)
  57. ends = ':%d' % (end+step)
  58. steps = '' if step == 1 else (':%d' % step)
  59. return 's[%s%s%s]' % (starts, ends, steps)
  60. step = None
  61. for i, prev in zip(idxs[1:], idxs[:-1]):
  62. if step is not None:
  63. if i - prev == step:
  64. continue
  65. yield _genslice(start, prev, step)
  66. step = None
  67. continue
  68. if i - prev in [-1, 1]:
  69. step = i - prev
  70. start = prev
  71. continue
  72. else:
  73. yield 's[%d]' % prev
  74. if step is None:
  75. yield 's[%d]' % i
  76. else:
  77. yield _genslice(start, i, step)
  78. def _assert_compress(inp, exp):
  79. res = list(compress(inp))
  80. if res != exp:
  81. print('Got %r, expected %r' % (res, exp))
  82. assert res == exp
  83. _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
  84. _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
  85. _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
  86. def gen(wrong, right, indent):
  87. code = ' + '.join(find_matching(wrong, right))
  88. return 'if len(s) == %d:\n%s return %s\n' % (len(wrong), indent, code)
  89. def genall(tests):
  90. indent = ' ' * 8
  91. return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
  92. def main():
  93. print(genall(tests))
  94. print(u' Age gate:')
  95. print(genall(tests_age_gate))
  96. if __name__ == '__main__':
  97. main()