Whats the simplest and safest method to generate a API KEY and SECRET in Python

后端 未结 5 845
遥遥无期
遥遥无期 2020-12-15 04:00

I need to generate a API key and Secret that would be stored in a Redis server. What would be the best way to generate a key and secret?

I am develop a Django-tastyp

5条回答
  •  醉酒成梦
    2020-12-15 04:39

    Adding answer as I can't comment on T. Opletals answer.

    You should not use random.choice as random isn't cryptographically secure. A better option would be random.SystemRandom() which uses the system source of randomness, on linux this would be urandom.

    def generate_key(length):
        char_set = string.ascii_letters + string.punctuation                    
        urand = random.SystemRandom()                                           
        return ''.join([urand.choice(char_set) for _ in range(length)])
    

提交回复
热议问题