1

I'd prefer not to change the current code I've already written despite it probably being really inefficient or whatever.

import string
import random

prompt=raw_input("Name")
print "Code:",prompt
a=string.ascii_letters+string.digits+string.digits
trip='!%s%s%s%s%s%s%s%s%s%s' % (random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a),random.choice(a))
print trip

What I want to do is make a tripcode generator so that this program I've written produces a code in the output that is always consistent with the input. So every time someone type a word in the prompt, they will always get the same randomly generated code.

So if someone typed "python" in the input, the output would always be !gT3rF39hjj or some randomly generated tripcode.

Sorry if I'm being unclear.

LillySatou
  • 11
  • 1

3 Answers3

2

What about using an hash or HMAC?

For example:

import base64
import hmac
import hashlib

prompt = raw_input('Name')
print 'Code:',prompt
trip = base64.b64encode(hmac.new('somekey', prompt, hashlib.sha1).digest())
print trip

As long as the key is kept secret the output would be unpredictable.

smeso
  • 4,165
  • 18
  • 27
  • +1. If by "random" the OP means "you can't guess the plain text if you only know the tripcode", then using a key is a good idea. That makes it more resistant than my answer, when it comes to brute-force attacks. – Kevin Dec 12 '13 at 18:50
1

Oops! My first answer was pretty silly -- it gives the same answer every time you pass a name, but only because it returns the same code every time. Instead, we can set the seed based on the name (see docs here). For example:

prompt=raw_input("Name")
random.seed(prompt)

produces

~/coding$ python tripcode.py 
NameFred
Code: Fred
!yjmjKnTwC8
~/coding$ python tripcode.py 
NameFred
Code: Fred
!yjmjKnTwC8
~/coding$ python tripcode.py
NameBob
Code: Bob
!C7im8B12jo

[BTW, prompt would be better named name, wouldn't it?]

DSM
  • 342,061
  • 65
  • 592
  • 494
1

You can use hashlib to generate almost-unique digests for the data the user inputs.

import hashlib

name = raw_input("Name: ")
trip = hashlib.md5(name).hexdigest()
print trip

Result:

C:\programs>get_trip.py
Name: Kevin
f1cd318e412b5f7226e5f377a9544ff7

C:\programs>get_trip.py
Name: George
578ad8e10dc4edb52ff2bd4ec9bc93a3

C:\programs>get_trip.py
Name: Kevin
f1cd318e412b5f7226e5f377a9544ff7
Kevin
  • 74,910
  • 12
  • 133
  • 166