2014-08-30 4 views
1

현재 요청 토큰을 보내 스크립트를 작성하려고하는데 헤더와 claimset이 있지만 서명을 이해하지 못합니다! OAuth는 내 개인 키가 SHA256withRSA (SHA-256 해시 기능이있는 RSASSA-PKCS1-V1_5-SIGN이라고도 함)로 암호화되어야하지만 가장 가까운 곳은 RSAES-PKCS1-v1_5 (RSA가 있고 SHA- 256 해시). 나는 예를 들어, 다음과 불통, 그래서 그것을 설정 얻을 수 있지만를 heres 내 dillema : 서명 = "" 시간 = SHA.new (서명)요청 토큰에 서명하는 방법은 무엇입니까?

key = RSA.importKey(open('C:\Users\Documents\Library\KEY\My Project 905320c6324f.json').read()) 
cipher = PKCS1_v1_5.new(key) 
ciphertext = cipher.encrypt(message+h.digest()) 
print(ciphertext) 

나는 약간의를 잃었어요 공개 키와 비공개 키가있는 JSON 파일에는 비공개 키를 복사하여 서명 변수에 붙여 넣을 수 있습니까 (구문이 잘못되었습니다)? 아니면 디렉토리를 다시 지나치나요? 나는 그렇게 길을 잃었다. 그리고 나의 머리 위에 haha. 현재 PyCrypto를 사용하여 Python 3.4를 실행하고 있습니다.

답변

0

gmail을 사용하여 명령 시스템을 작성하고자하는 내용에 따라 IMAP을 사용하여 간단한 스크립트를 작성했습니다. 나는 이것이 운동을 위해 간단하게하기를 원하지 않는 한, 단일 사용자를 위해 Google API를 사용하는 것보다 아마도 간단하다고 생각합니다.

import imaplib, logging 
from time import sleep 

USERNAME = 'YOUR_USERNAME_HERE' # For gmail, this is your full email address. 
PASSWORD = 'YOUR_PASSWORD_HERE' 
CHECK_DELAY = 60 # In seconds 

LOGGING_FORMAT = '%(asctime)s %(message)s' 
logging.basicConfig(filename='imapTest.log', format=LOGGING_FORMAT, level=logging.INFO) 

logging.info("Connecting to IMAP server...") 
imap = imaplib.IMAP4_SSL('imap.gmail.com') 
imap.login(USERNAME, PASSWORD) 
logging.info("Connected to IMAP server.") 

def get_command_messages(): 
    logging.info("Checking for new commands.") 
    imap.check() 
    # Search the inbox (server-side) for messages containing the subject 'COMMAND' and which are from you. 
    # Substitute USERNAME below for the sending email address if it differs. 
    typ, data = imap.search(None, '(FROM "%s" SUBJECT "COMMAND")' %(USERNAME)) 
    return data[0] 

def delete_messages(message_nums): 
    logging.info("Deleting old commands.") 
    for message in message_nums.split(): 
     imap.store(message, '+FLAGS', '\\DELETED') 
    imap.expunge() 

# Select the inbox 
imap.select() 

# Delete any messages left over that match commands, so we are starting 'clean'. 
# This probably isn't the nicest way to do this, but saves checking the DATE header. 
message_nums = get_command_messages() 
delete_messages(message_nums) 

try: 
    while True: 
     sleep(CHECK_DELAY) 
     # Get the message body and sent time. Use BODY.PEEK instead of BODY if you don't want to mark the message as read, but we're deleting it anyway below. 
     message_nums = get_command_messages() 
     if message_nums: 
      # search returns space-separated message IDs, but we need them comma-separated for fetch. 
      typ, messages = imap.fetch(message_nums.replace(' ', ','), '(BODY[TEXT])') 
      logging.info("Found %d commands" %(len(messages[0]))) 
      for message in messages[0]: 
       # You now have the message body in the message variable. 
       # From here, you can check against it to perform commands, e.g: 
       if 'shutdown' in message: 
        print("I got a shutdown command!") 
        # Do stuff 
      delete_messages(message_nums) 
finally: 
    try: 
     imap.close() 
    except: 
     pass 
    imap.logout() 

당신은 Gmail의 API를 사용하여 설정하는 경우,하지만, 구글이 강력하게 기존의 파이썬 라이브러리를 사용하기보다는 당신이로 나타나는 등 자신을 완전한 인증을 시도하는 것이 좋습니다. 따라서 위의 imap 호출을 관련 Gmail API로 대체해야 할 수도 있습니다.

+0

Google 개발자 사이트에서 말한 내용을 벗어나 RSASHA256으로 서명해야하는 것 외에 다른 많은 정보를 제공하지 않습니다. 설명에 대한 링크가 있습니까? – Aflake

+0

[Google Python 라이브러리] (https://developers.google.com/api-client-library/python/guide/aaa_oauth)를 사용해도 되겠습니까? 그렇다면, 그 페이지는 그것을 사용하는 방법을 설명하고 필요한 경우 oauth 클라이언트 만 다운로드 할 수있는 링크를 제공합니다. 특히, 나는'flow_from_clientsecrets()'메소드가 당신이 필요로하는 것으로 의심된다. 그 외에도 특정 Google API로 무엇을하고 싶은지 모른 채 도움을받을 수 있는지 확신 할 수 없습니다. – Kkelk

+0

서비스 계정을 설정하고 Gmail API를 사용하여 "command : reboot"와 같은 텍스트를 사용하여 명령을 보내려고 texing (전자 메일)을 통해 내 PC와 전화를 연결할 계획이었습니다. api 및 더 큰 프로젝트와 상호 작용하는 데 도움이되는 스크립트가 다소 있습니다. 내 문제는 JWT에 서명해야하기 때문에 토큰 요청과 관련이 있습니다. – Aflake