1

I am working on my first real coding project and would like to create a tool to encode my login information for my personal VPN based on the date and possibly time. I'm doing this so that even if I log into my server with compromised wifi my server would still be safe.

from datetime import date
today = date.today()
today = str(today).replace('-', '')


import hashlib


imput = "password"
h = hashlib.sha256(imput.encode(today))


hash = h.hexdigest()
print(hash)

The error I get while working on this is:

h = hashlib.sha256(imput.encode(today))
LookupError: unknown encoding: 20201017

I know this is a very basic issue but I am just barely learning so any help getting this functional would be great.

werner
  • 13,518
  • 6
  • 30
  • 45
  • 1
    Welcome to Stackoverflow. You can increase the chance of getting an answer if you edit your question and additionally tag the programming language you are using because many experts see only "their" language. Regarding your problem: The output of your today variable seems to be a string that is not in the right format for your sha256-hash - one idea would be to search for e.g. "python sha256 eample". – Michael Fehr Oct 17 '20 at 08:29

1 Answers1

0

str.encode() accepts as encoding one of the available standard encodings. You are passing the string 20201017 which results in the error message.

Instead you can pass any of the available encodings or simply none if you want to use the standard encoding utf-8.

h = hashlib.sha256(imput.encode())
hash = h.hexdigest()
print(hash)

prints

5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

If you don't like to use utf-8 you can also convert the string into a byte literal.

h = hashlib.sha256(b"imput")
hash = h.hexdigest()
print(hash)

prints

c8437f4785336512556fea9d3f32c319e2674a610872a14fc33876aa116d7247
werner
  • 13,518
  • 6
  • 30
  • 45