Posts tagged Java

A Java Class to upload files trough FTP

0
Download Ftp.java
/**
* FILE: Ftp.java
* DESCRIPTION: A ftp uploader GUI class
* LIMITATION: Uses AWT instead of SWING
* VERSION: 09-Feb-2006
*
* Olaf says:
*
* -- Remenber, all is posible but the problem
* is the time you are able to spend doing it. --
*/

import java.io.*;
import java.net.*;
import java.awt.*;

/**
* An ftp uploader class (GUI File Selector)
* @author Olaf Reitmaier (olafrv@gmail.com)
*/
public class Ftp{

public static void main(String args[]) throws Exception{

int PIECE_SIZE = 7000; //Byte transfer rate (56 Kbps = 7KB)
int bytes = 0; //Temp var of read bytes
byte[] piece = new byte[PIECE_SIZE]; //Temp var of file pieces

String server = "a_server_ip"; //The ftp server
String user = "an_user"; //The ftp user
String password = "a_passwd"; //The ftp password

String[] files=selectFile();

if(files[0]!=null && files[1]!=null){

String localFile = files[0]+File.separator+files[1];
String remoteFile = "/"+files[1];

String strUrl = "ftp://"+user+":"+password+"@"+server+remoteFile+";type=i";

URL url = new URL(strUrl);
URLConnection urlc = url.openConnection();

FileInputStream fis = new FileInputStream(new File(localFile));
OutputStream os = urlc.getOutputStream();

while(true){
bytes = fis.available();
if (bytes<=0){ break; }else{ if (bytes>=PIECE_SIZE){
fis.read(piece,0,PIECE_SIZE);
os.write(piece,0,PIECE_SIZE);
}else{
fis.read(piece,0,bytes);
os.write(piece,0,bytes);
}
}
}
os.close(); //Close the remote file stream
fis.close(); //Close the local file stream
}
}

/**
* An AWT File Chooser
* Courtesy of Jose Carrero <josercl@gmail.com>

* @return A string array with the first position

* containing the fullpath of the selected file

*/

public static String[] selectFile(){

String[] files=new String[2];

Frame frame=new Frame();

FileDialog file_chooser=new FileDialog(frame);

file_chooser.show();

files[0]=file_chooser.getDirectory();

files[1]=file_chooser.getFile();

file_chooser.dispose(); //Needed to terminates the program

frame.dispose(); //Needed to terminates the program

return files;

}

}

MD5, SHA-1 hash sums in Java 2

0
Download HashSum.java
/**
* A class for calculating hash sum in files or strings
* using all the hash algorithms supported by Java
*
* @author Olaf Reitmaier
* @version December, 2005
*/
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;

import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;

public class HashSum{

public static final int BUFFER_SIZE = 1024; //The maximun buffer size

private MessageDigest msgDigest; //The message digest processor
private String hashStr; //The result sum of the digest commited with the hash algoritm

/**
* Creates a new hash processor
* @param algorithm The name of the algorithm (MD5, SHA-1, SHA-256, SHA-512, etc). See Java 2 documentacion.
*/
public HashSum(String algorithm) throws NoSuchAlgorithmException{
msgDigest = MessageDigest.getInstance(algorithm);
}

/**
* Calculate the hash sum of a file
* @param srcFile The file to be processed
* @return A byte array with the hash sum value (Must be converted to Hexadecimal)
*/
public byte [] calculate(File srcFile) throws IOException{
boolean ready = false;
int bytesOnBuffer = 0;
byte[] text = null;
FileInputStream fis = new FileInputStream(srcFile);
msgDigest.reset(); //Initialize the hashing proccessor
while(!ready){
bytesOnBuffer = fis.available(); //Available bytes on buffer
if (bytesOnBuffer >= BUFFER_SIZE){ // If there are too much bytes
text = new byte[BUFFER_SIZE]; //Create a new fixed buffer
fis.read(text, 0, BUFFER_SIZE);
msgDigest.update(text); //Hash
}else{
//If there are few bytes
if (bytesOnBuffer > 0){ //
text = new byte[bytesOnBuffer]; //Create a new fixed buffer
fis.read(text, 0, bytesOnBuffer); //Read all the last bytes
msgDigest.update(text); //Hash the last bytes
}
ready = true;
}
}
fis.close();
if (ready){
return msgDigest.digest(); //Final operations (padding)
}else{
return null; //No digest was done!!!
}
}

/**
* Calculate the hash sum of a string
* @param str The string to be processed
* @return A byte array with the hash sum value (Must be converted to Hexadecimal)
*/
public byte [] calculate(String str){
msgDigest.reset();
msgDigest.update(str.getBytes());
return msgDigest.digest();
}

/**
* Converts the byte array to string (Version #1)
* @param bytes The byte array
* @return A lower case string in hexadecimal notation
*/
public static String toHexString1(byte [] bytes) {
StringBuffer sb = new StringBuffer();
String s = null;
for (int i=0;i< s = "0"> 2) s = s.substring(s.length()-2);
sb.append(s);
}
return sb.toString();
}

/**
* Converts the byte array to string (Version #2)
* @param bytes The byte array
* @return A lower case string in hexadecimal notation
*/
public static String toHexString2(byte [] bytes) {
String HEX_DIGITS = "0123456789abcdef";
StringBuffer sb = new StringBuffer(bytes.length * 2);
int bValue = 0;
for (int i = 0; i < bvalue =" bytes[i]">>> 4))
.append(HEX_DIGITS.charAt(bValue & 0xF));
}
return sb.toString();
}
}

Blowfish Cipher Algorithm in Java

2

Download source code here: Blowfish.tar.gz

An implementation on Blowfish cipher algorithm in Java, based on:

* Bruce Schneier (2009)
* Dr. Herong Yang (2009)
* Wikipedia (2009)
* DI Managment (2009)

Copyright (C) Dec 23th, 2009 – Olaf Reitmaier Veracierta

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see .

Go to Top
?>