To encrypt a password or any other string
you do it in the application or in the bank. Here I will deal with SHA1 and MD5, but the latter is no longer recommended, see How to hash passwords securely?.
Postgressql
MD5
SELECT md5(senha);
SHA1
The MD5 algorithm is ready to be used in your bank, but the SHA1 is not.
To be used the extension must be created pg_crypto with his schema selected and then can be used the function digest
which also has algorithms other than SHA1.
CREATE EXTENSION pgcrypto;
SELECT encode(digest('senha', 'sha1'), 'hex');
Mysql
MD5
SELECT md5('senha');
SHA1
SELECT sha1('senha');
Java
// MD5
String criptografadaMd5 = criptografar("123456", "MD5");
// SHA1
String criptografadaSha1 = criptografar("123456", "SHA1");
public static String cripografar(String input, String tipoAlgoritmo) throws NoSuchAlgorithmException {
MessageDigest mDigest = MessageDigest.getInstance(tipoAlgoritmo);
byte[] result = mDigest.digest(input.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
The type of encryption that protects passwords is called hash. The idea is not to save the password itself in the database, but the result of applying a "one-way" function to the password (i.e. easy to do, hard to undo). To check later if the password is correct, the hash and compare the results. In practice, you will want to use hashes ""strong, like PBKDF2, Bcrypt or scrypt, but for the purpose of this exercise you can use what the bank even offers you, I don’t see the need to go much further. But if you can try using one salt, this will be important after "in the real world".
– mgibsonbr