Skip to content

Commit

Permalink
activate sourcecode formatting (mwiede#247)
Browse files Browse the repository at this point in the history
* activate sourcecode formatting
  • Loading branch information
mwiede authored Feb 9, 2023
1 parent 886f38e commit 72f8a6d
Show file tree
Hide file tree
Showing 317 changed files with 29,304 additions and 29,377 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"java.configuration.updateBuildConfiguration": "automatic",
"terminal.integrated.defaultProfile.linux": "bash"
"terminal.integrated.defaultProfile.linux": "bash",
"java.checkstyle.configuration": "/google_checks.xml"
}
337 changes: 337 additions & 0 deletions eclipse-java-google-style.xml

Large diffs are not rendered by default.

148 changes: 69 additions & 79 deletions examples/AES.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
* This program will demonstrate how to use "aes128-cbc".
*
Expand All @@ -7,31 +6,29 @@
import java.awt.*;
import javax.swing.*;

public class AES{
public static void main(String[] arg){
public class AES {
public static void main(String[] arg) {

try{
JSch jsch=new JSch();
try {
JSch jsch = new JSch();

//jsch.setKnownHosts("/home/foo/.ssh/known_hosts");
// jsch.setKnownHosts("/home/foo/.ssh/known_hosts");

String host=null;
if(arg.length>0){
host=arg[0];
String host = null;
if (arg.length > 0) {
host = arg[0];
} else {
host = JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name") + "@localhost");
}
else{
host=JOptionPane.showInputDialog("Enter username@hostname",
System.getProperty("user.name")+
"@localhost");
}
String user=host.substring(0, host.indexOf('@'));
host=host.substring(host.indexOf('@')+1);
String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);

Session session = jsch.getSession(user, host, 22);
// session.setPassword("your password");

Session session=jsch.getSession(user, host, 22);
//session.setPassword("your password");

// username and password will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);

session.setConfig("cipher.s2c", "aes128-cbc,3des-cbc,blowfish-cbc");
Expand All @@ -40,63 +37,61 @@ public static void main(String[] arg){

session.connect();

Channel channel=session.openChannel("shell");
Channel channel = session.openChannel("shell");

channel.setInputStream(System.in);
channel.setOutputStream(System.out);

channel.connect();
}
catch(Exception e){
} catch (Exception e) {
System.out.println(e);
}
}

public static class MyUserInfo implements UserInfo, UIKeyboardInteractive{
public String getPassword(){ return passwd; }
public boolean promptYesNo(String str){
Object[] options={ "yes", "no" };
int foo=JOptionPane.showOptionDialog(null,
str,
"Warning",
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null, options, options[0]);
return foo==0;
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public String getPassword() {
return passwd;
}

public boolean promptYesNo(String str) {
Object[] options = {"yes", "no"};
int foo = JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
return foo == 0;
}

String passwd;
JTextField passwordField=(JTextField)new JPasswordField(20);

public String getPassphrase(){ return null; }
public boolean promptPassphrase(String message){ return true; }
public boolean promptPassword(String message){
Object[] ob={passwordField};
int result=
JOptionPane.showConfirmDialog(null, ob, message,
JOptionPane.OK_CANCEL_OPTION);
if(result==JOptionPane.OK_OPTION){
passwd=passwordField.getText();
JTextField passwordField = (JTextField) new JPasswordField(20);

public String getPassphrase() {
return null;
}

public boolean promptPassphrase(String message) {
return true;
}

public boolean promptPassword(String message) {
Object[] ob = {passwordField};
int result = JOptionPane.showConfirmDialog(null, ob, message, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
passwd = passwordField.getText();
return true;
}
else{
return false;
} else {
return false;
}
}
public void showMessage(String message){

public void showMessage(String message) {
JOptionPane.showMessageDialog(null, message);
}
final GridBagConstraints gbc =
new GridBagConstraints(0,0,1,1,1,1,
GridBagConstraints.NORTHWEST,
GridBagConstraints.NONE,
new Insets(0,0,0,0),0,0);

final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
private Container panel;
public String[] promptKeyboardInteractive(String destination,
String name,
String instruction,
String[] prompt,
boolean[] echo){

public String[] promptKeyboardInteractive(String destination, String name, String instruction,
String[] prompt, boolean[] echo) {
panel = new JPanel();
panel.setLayout(new GridBagLayout());

Expand All @@ -108,39 +103,34 @@ public String[] promptKeyboardInteractive(String destination,

gbc.gridwidth = GridBagConstraints.RELATIVE;

JTextField[] texts=new JTextField[prompt.length];
for(int i=0; i<prompt.length; i++){
JTextField[] texts = new JTextField[prompt.length];
for (int i = 0; i < prompt.length; i++) {
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.weightx = 1;
panel.add(new JLabel(prompt[i]),gbc);
panel.add(new JLabel(prompt[i]), gbc);

gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weighty = 1;
if(echo[i]){
texts[i]=new JTextField(20);
}
else{
texts[i]=new JPasswordField(20);
if (echo[i]) {
texts[i] = new JTextField(20);
} else {
texts[i] = new JPasswordField(20);
}
panel.add(texts[i], gbc);
gbc.gridy++;
}

if(JOptionPane.showConfirmDialog(null, panel,
destination+": "+name,
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE)
==JOptionPane.OK_OPTION){
String[] response=new String[prompt.length];
for(int i=0; i<prompt.length; i++){
response[i]=texts[i].getText();
if (JOptionPane.showConfirmDialog(null, panel, destination + ": " + name,
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
String[] response = new String[prompt.length];
for (int i = 0; i < prompt.length; i++) {
response[i] = texts[i].getText();
}
return response;
}
else{
return null; // cancel
} else {
return null; // cancel
}
}
}
Expand Down
77 changes: 35 additions & 42 deletions examples/ChangePassphrase.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,64 @@
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/**
* This program will demonstrate to change the passphrase for a
* private key file instead of creating a new private key.
* $ CLASSPATH=.:../build javac ChangePassphrase.java
* $ CLASSPATH=.:../build java ChangePassphrase private-key
* A passphrase will be prompted if the given private-key has been
* encrypted. After successfully loading the content of the
* private-key, the new passphrase will be prompted and the given
* private-key will be re-encrypted with that new passphrase.
* This program will demonstrate to change the passphrase for a private key file instead of creating
* a new private key. $ CLASSPATH=.:../build javac ChangePassphrase.java $ CLASSPATH=.:../build java
* ChangePassphrase private-key A passphrase will be prompted if the given private-key has been
* encrypted. After successfully loading the content of the private-key, the new passphrase will be
* prompted and the given private-key will be re-encrypted with that new passphrase.
*
*/
import com.jcraft.jsch.*;
import javax.swing.*;

class ChangePassphrase{
public static void main(String[] arg){
if(arg.length!=1){
class ChangePassphrase {
public static void main(String[] arg) {
if (arg.length != 1) {
System.err.println("usage: java ChangePassphrase private_key");
System.exit(-1);
}

JSch jsch=new JSch();
JSch jsch = new JSch();

String pkey=arg[0];
String pkey = arg[0];

try{
KeyPair kpair=KeyPair.load(jsch, pkey);
try {
KeyPair kpair = KeyPair.load(jsch, pkey);

System.out.println(pkey+" has "+(kpair.isEncrypted()?"been ":"not been ")+"encrypted");
System.out
.println(pkey + " has " + (kpair.isEncrypted() ? "been " : "not been ") + "encrypted");

String passphrase="";
while(kpair.isEncrypted()){
JTextField passphraseField=(JTextField)new JPasswordField(20);
Object[] ob={passphraseField};
int result=JOptionPane.showConfirmDialog(null, ob,
"Enter passphrase for "+pkey,
JOptionPane.OK_CANCEL_OPTION);
if(result!=JOptionPane.OK_OPTION){
String passphrase = "";
while (kpair.isEncrypted()) {
JTextField passphraseField = (JTextField) new JPasswordField(20);
Object[] ob = {passphraseField};
int result = JOptionPane.showConfirmDialog(null, ob, "Enter passphrase for " + pkey,
JOptionPane.OK_CANCEL_OPTION);
if (result != JOptionPane.OK_OPTION) {
System.exit(-1);
}
passphrase=passphraseField.getText();
if(!kpair.decrypt(passphrase)){
System.out.println("failed to decrypt "+pkey);
}
else{
System.out.println(pkey+" is decrypted.");
passphrase = passphraseField.getText();
if (!kpair.decrypt(passphrase)) {
System.out.println("failed to decrypt " + pkey);
} else {
System.out.println(pkey + " is decrypted.");
}
}

passphrase="";
passphrase = "";

JTextField passphraseField=(JTextField)new JPasswordField(20);
Object[] ob={passphraseField};
int result=JOptionPane.showConfirmDialog(null, ob,
"Enter new passphrase for "+pkey+
" (empty for no passphrase)",
JOptionPane.OK_CANCEL_OPTION);
if(result!=JOptionPane.OK_OPTION){
JTextField passphraseField = (JTextField) new JPasswordField(20);
Object[] ob = {passphraseField};
int result = JOptionPane.showConfirmDialog(null, ob,
"Enter new passphrase for " + pkey + " (empty for no passphrase)",
JOptionPane.OK_CANCEL_OPTION);
if (result != JOptionPane.OK_OPTION) {
System.exit(-1);
}
passphrase=passphraseField.getText();
passphrase = passphraseField.getText();

kpair.setPassphrase(passphrase);
kpair.writePrivateKey(pkey);
kpair.dispose();
}
catch(Exception e){
} catch (Exception e) {
System.out.println(e);
}
System.exit(0);
Expand Down
Loading

0 comments on commit 72f8a6d

Please sign in to comment.