Encrypt text string with SHA1

2

I have the following code in Java:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class EnconderPass {

public static void main(String[] args) {
    String password = "12345";
    byte[] newPassword = null;
    try {
        newPassword = MessageDigest.getInstance("SHA").digest(password.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    String encriptado = Base64.getEncoder().encodeToString(newPassword);
    System.out.println(encriptado);

}

}

I want to reproduce the same results with Python 3, but I do not know how to "translate" it. In this Java example the string 1235 is equivalent to jLIjfQZ5yojbZGTqxg2pY0VROWQ =

How could I do it in Python 3?

    
asked by Reynald0 23.07.2017 в 23:43
source

1 answer

2

Basically they are two libraries to use, hashlib and base64 . The basic thing would be:

import hashlib
import base64


password = "12345"
newpassword = hashlib.sha1(password.encode()).digest()
encriptado = base64.b64encode(newpassword).decode('UTF-8')
print(encriptado)

Exit:

  

jLIjfQZ5yojbZGTqxg2pY0VROWQ =

It only remains to put it in a class and handle the exceptions with a try-except if you want a "literal" translation.

    
answered by 24.07.2017 / 00:22
source