生成otp token 脚本

#!/usr/bin/python3
 
import re
import time
import hashlib
import struct
import base64
import hmac
from random import choice
from string import ascii_letters, digits
from sys import argv

 

secret = '这里是你的secret'

 

mangling_string = 'ASGIw3gQMAbEw6z4u8xxmfwYx5qG4b16'

 

def get_hotp_token(secret, intervals_no):
key = base64.b32decode(secret, True)
msg = struct.pack(">Q", intervals_no)
h = hmac.new(key, msg, hashlib.sha1).digest()
o = h[19] & 15
h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
return h



def get_totp_token(secret):
return get_hotp_token(secret, intervals_no=int(time.time())//30)



def xor_crypt_string(data, key='awesomepassword', encode=False, decode=False):
from sys import byteorder
import base64
if decode:
data = base64.decodebytes(bytearray(data, 'ascii'))
else:
data = bytearray(data, 'ascii')
while (len(data) > len(key)):
key += key
key = key[:len(data)]
key = bytearray(key, 'ascii')
int_data = int.from_bytes(data, byteorder)
int_key = int.from_bytes(key, byteorder)
int_enc = int_data ^ int_key
int_enc = int_enc.to_bytes(len(data), byteorder)
if encode:
return base64.encodebytes(int_enc).strip()
return int_enc



# extract params:
if (len(argv) > 1):
secret = argv[1]

 

try:
token = get_totp_token(xor_crypt_string(
secret, mangling_string, decode=True))
except:
print("Invalid Secret")

 

token = '123456' + '%06d' % token
print(token)
 
 
 
## https://github.com/vitorgalvao/custom-alfred-iterm-scripts

 

posted @ 2021-01-28 14:58  花泪哲  阅读(523)  评论(0编辑  收藏  举报