| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.samples; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.DriverManager; |
| 10 |
import java.sql.PreparedStatement; |
| 11 |
import java.sql.ResultSet; |
| 12 |
import java.sql.Statement; |
| 13 |
import java.util.Properties; |
| 14 |
|
| 15 |
/** |
| 16 |
* This example shows how to secure passwords |
| 17 |
* (both database passwords, and account passwords). |
| 18 |
*/ |
| 19 |
public class SecurePassword { |
| 20 |
|
| 21 |
/** |
| 22 |
* This method is called when executing this sample application from the |
| 23 |
* command line. |
| 24 |
* |
| 25 |
* @param args the command line parameters |
| 26 |
* @throws Exception on failure |
| 27 |
*/ |
| 28 |
public static void main(String... args) throws Exception { |
| 29 |
|
| 30 |
Class.forName("org.h2.Driver"); |
| 31 |
String url = "jdbc:h2:data/simple"; |
| 32 |
String user = "sam"; |
| 33 |
char[] password = {'t', 'i', 'a', 'E', 'T', 'r', 'p'}; |
| 34 |
|
| 35 |
// This is the normal, but 'unsafe' way to connect: |
| 36 |
// the password may reside in the main memory for an undefined time, |
| 37 |
// or even written to disk (swap file): |
| 38 |
// Connection conn = |
| 39 |
// DriverManager.getConnection(url, user, new String(password)); |
| 40 |
|
| 41 |
// This is the most safe way to connect: the password is overwritten |
| 42 |
// after use |
| 43 |
Properties prop = new Properties(); |
| 44 |
prop.setProperty("user", user); |
| 45 |
prop.put("password", password); |
| 46 |
Connection conn = DriverManager.getConnection(url, prop); |
| 47 |
|
| 48 |
// For security reasons, account passwords should not be stored directly |
| 49 |
// in a database. Instead, only the hash should be stored. Also, |
| 50 |
// PreparedStatements must be used to avoid SQL injection: |
| 51 |
Statement stat = conn.createStatement(); |
| 52 |
stat.execute( |
| 53 |
"drop table account if exists"); |
| 54 |
stat.execute( |
| 55 |
"create table account(" + |
| 56 |
"name varchar primary key, " + |
| 57 |
"salt binary default secure_rand(16), " + |
| 58 |
"hash binary)"); |
| 59 |
PreparedStatement prep; |
| 60 |
prep = conn.prepareStatement("insert into account(name) values(?)"); |
| 61 |
prep.setString(1, "Joe"); |
| 62 |
prep.execute(); |
| 63 |
prep.close(); |
| 64 |
|
| 65 |
prep = conn.prepareStatement( |
| 66 |
"update account set " + |
| 67 |
"hash=hash('SHA256', stringtoutf8(salt||?), 10) " + |
| 68 |
"where name=?"); |
| 69 |
prep.setString(1, "secret"); |
| 70 |
prep.setString(2, "Joe"); |
| 71 |
prep.execute(); |
| 72 |
prep.close(); |
| 73 |
|
| 74 |
prep = conn.prepareStatement( |
| 75 |
"select * from account " + |
| 76 |
"where name=? " + |
| 77 |
"and hash=hash('SHA256', stringtoutf8(salt||?), 10)"); |
| 78 |
prep.setString(1, "Joe"); |
| 79 |
prep.setString(2, "secret"); |
| 80 |
ResultSet rs = prep.executeQuery(); |
| 81 |
while (rs.next()) { |
| 82 |
System.out.println(rs.getString("name")); |
| 83 |
} |
| 84 |
rs.close(); |
| 85 |
prep.close(); |
| 86 |
stat.close(); |
| 87 |
conn.close(); |
| 88 |
} |
| 89 |
|
| 90 |
} |