3 # BorkNet Services Core
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 import java
.security
.*;
30 public static void main(String args
[])
32 CreateAccount ca
= new CreateAccount();
35 public CreateAccount()
38 Connection con
= openSqlConnection();
39 createUserAccount(con
);
40 closeSqlConnection(con
);
41 System
.out
.println("Useraccount added!");
45 private void testDriver()
49 Class
.forName ( "org.gjt.mm.mysql.Driver" );
51 catch ( java
.lang
.ClassNotFoundException e
)
53 System
.out
.println("MySQL JDBC Driver not found!");
58 private Connection
openSqlConnection()
62 String server
= readFromConsole("SQL Server host:");
63 String username
= readFromConsole("SQL username:");
64 String password
= readFromConsole("password:");
65 String database
= readFromConsole("Database:");
66 String url
= "jdbc:mysql://"+server
+"/"+database
+"?user="+username
+"&password="+password
;
67 Connection con
= DriverManager
.getConnection(url
);
72 System
.out
.println("Could not open SQL connection!");
78 private void createUserAccount(Connection con
)
82 PreparedStatement pstmt
;
83 pstmt
= con
.prepareStatement("INSERT INTO auths VALUES ('',?,?,?,?,?,?,?)");
84 pstmt
.setString(1,readFromConsole("Bot Account Username:"));
85 pstmt
.setString(2,encrypt(readFromConsole("Bot Account Password:")));
86 pstmt
.setString(3,readFromConsole("Bot Account E-Mail:"));
87 pstmt
.setString(4,"1000");
88 pstmt
.setString(5,"false");
89 pstmt
.setString(6,"0");
90 pstmt
.setString(7,"0");
91 pstmt
.executeUpdate();
93 catch ( SQLException e
)
95 System
.out
.println ( "Error executing sql statement" );
100 private String
readFromConsole(String question
)
102 System
.out
.println(question
);
103 Scanner in
= new Scanner(System
.in
);
104 return in
.nextLine();
107 //con = getConnection ( server, user, password, db);
108 private void closeSqlConnection(Connection con
)
116 System
.out
.println("MySQL connection failed to close.");
121 public String
encrypt(String plaintext
)
123 byte[] defaultBytes
= plaintext
.getBytes();
126 MessageDigest algorithm
= MessageDigest
.getInstance("MD5");
128 algorithm
.update(defaultBytes
);
129 byte messageDigest
[] = algorithm
.digest();
130 StringBuffer hexString
= new StringBuffer();
131 for (int i
=0;i
<messageDigest
.length
;i
++)
133 String hex
= Integer
.toHexString(0xFF & messageDigest
[i
]);
134 if(hex
.length()==1) hexString
.append('0');
135 hexString
.append(hex
);
137 return hexString
.toString();
139 catch(NoSuchAlgorithmException e
)
141 System
.out
.println ( "Error encrypting password." );