Programming

How-to Display a Debian Server’s RSA Fingerprint

Need to verify a rsa host fingerprint for a debian server? Have someone connect and run the following command:

ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key

Generating Thumbnails from FLV using FFMPEG

I had to make some modifications to a zenGallery system which was going to hold a lot of flash video files and I was too lazy to create a thumbnail for each image so for awhile it had the default thumbnail.

After a few, “what’s with the thumbnails?” I decided to write a script that executed ffmpeg to create a thumbnail for each video (from the frame 10 seconds into the video) and write out a jpg image.

The filename currently just replaces .flv with .jpg and my settings for the call are pretty generic. If anyone has questions about this feel free to ask.

Grab the .phps file from here: Generate Thumbnails from FLV files using FFMPEG

-bash: /bin/rm: Argument list too long (AKA: Too Many Files)

find /path/to/directory/ -exec rm {} \;

So apparently that command is magical! Alright, so it’s not magical but it was able to delete over 200,000 spam messages from a users Maildir/ when the rm command failed. “-bash: /bin/rm: Argument list too long”, it complained! So asking around I found out about that beauty of a command and how rm can only take a certain amount of arguments before failing.

Nice to know, as my journey of learning more about Debian Linux (and Linux in general) continues.

MD5 with Dynamic Salt Class

Remember my post about MD5 Hashing and Salt? Well I’ve taken some time to develop a PHP class which helps very easily implement dynamic salt when using MD5. This class can be easily expanded or modified to use a different hash function. Take a look at it and let me know if it becomes useful to anyone!

You can download the PasswordWithSalt.class.php file here. Also I setup a VERY simple demo of how this works. I’m still working on developing this but comments and advice will definately be useful here.

 The following is the rough text and instructions I’ve written for my “readme.txt” file.

The only thing you need to do in order to properly configure this class is set the location you want to store your salts at. For example in the class set $storageLocation = “/home/user/salts/”; and give permissions for your server to write to that folder  (777 will work). I recommend the folder to be one outside your normal webroot.

The following demonstrates how to store salt for your user, then generate the hash using those salts. Don’t forget to store the final hash so you can compare against it later!

<?php
require(“PasswordWithSalt.class.php”);

//Init the class
$SaltPlease = new PasswordWithSalt();

//Store generated salts for user “admin”
$SaltPlease->storeSalt(“admin”);

//Get hash for user admin, password is “password”
$hash = $SaltPlease->createHash(“admin”, “password”);
?>

 

The following demonstrates how to duplicate the hash for a user who already has salt. $dbhash should be filled with the hash you have stored before.

<?php
require(“PasswordWithSalt.class.php”);

//Init the class
$SaltPlease = new PasswordWithSalt();

//Get hash for user admin, password is “password”
$hash = $SaltPlease->createHash(“admin”, “password”);

//Check to see if the generated hash match.
if($dbhash == $hash) {
echo “Password Hashes Match.”;
} else {
echo “Password Hashes DO NOT Match”;
}
?>

MD5 Hashing and Salt

I just read a great post by Marcel Oelke who runs http://md5.rednoize.com/. He’s got a great way to access his webservice and then check if a user’s password is insecure. Even if you are using MD5 before storing the password, I certianly hope people aren’t storing passwords in cleartext, it may not be secure. More >

Image Resizing Using PHP and the GD library

I’m developing a new system core, can’t really say what it’s about but I’m having some fun doing it. Coding it is interesting, I’m getting to pull out and update some of my php code. Below is some code for image resizing, I use config files to set some default information like directories to save to and max width/height requirements. Also the entire system uses language files so I don’t output the text directly either, except for what I’m going to remove which is the uploaded file information.

Resize JPG images using the GD library and PHP.

function get_image_information($key) {
global $thumbnail_dest, $image_dest, $thumb_max_height, $thumb_max_width, $image_max_height, $image_max_width;
$filename = $_FILES[$key]["name"];
$file_type = $_FILES[$key]["type"];
$file_tmpname = $_FILES[$key]["tmp_name"];
$file_error = $_FILES[$key]["error"];
$file_size = $_FILES[$key]["size"];

if($file_error == 0 && $file_size > 0) {
if($file_type == “image/pjpeg” $file_type == “image/jpeg” $file_type == “image/jpg”) {
$image_name = md5(time() . rand(1001,10000)) . “.jpg”;
$thumb_dest = $thumbnail_dest . $image_name;
$image_dest = $image_dest . $image_name;
create_image($file_tmpname, $thumb_max_height, $thumb_max_width, $thumb_dest);
create_image($file_tmpname, $image_max_height, $image_max_width, $image_dest);
return “thumbs/” . $image_name;
} else {
$success .= file_upload_success_1 . $filename . file_upload_success_2 . “
“;
$success .= “We can only accept jpg uploads. Upload Failed.”;
return $success;
}
} else {
if($file_error == 1) {
echo $file_upload_fail_1;
} elseif($file_error == 2) {
echo $file_upload_fail_2;
} elseif($file_error == 3) {
echo $file_upload_fail_3;
} elseif($file_error == 4) {
echo $file_upload_fail_4;
} elseif($file_error == 6) {
echo $file_upload_fail_6;
}
return false;
}
}
function create_image($image, $max_width, $max_height, $dest) {
$image = imagecreatefromjpeg($image);
if ($image === false) {
die (‘Unable to open image’);
}

$width = imagesx($image);
$height = imagesy($image);

if($width < $max_width && $height < $max_height) {
$new_width = $width;
$new_height = $height;
} else {
$scale = min($max_width/$width, $max_height/$height);
$new_width = floor($scale * $width);
$new_height = floor($scale * $height);
}

$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_resized, $dest, 90);
}

The XML Document Object Model

I’m writing a bunch of functions that communicate with Amazon using classic asp, it requires me to learn much more about the XMLDOM, so that I don’t manually parse through every XML file. For anyone else who is interested in the XMLDOM I recommend the article, “XML“, over at Dev Articles who, by the way, tend to produce many many very helpful and articulate articles written in a manner most people can either understand or learn to understand.

JJ

How To Do a Search Through Folders – Two Levels Deep

I used the following code to search through my “galleries” folder and search for images through every folder inside galleries sub-folders.

Huh?

I used each folder inside of galleries as the “category” for each set of images. What it ended up doing was this. Each time I wanted to create a new category I would just make a new folder inside of “galleries”, each time I wanted a new album I’d put another folder inside each category folder. For example I could have a folder structure as follows:
/galleries
—/wedding
——-/getting_ready
——-/court_house
——-/reception
—/first_home
——-/before
——-/after

As you can see my categories are under galleries and they are called “wedding” and “first_home”. My galleries are called “getting_ready”, “court_house”, “reception”, “before” and “after”.

Oh I see!

I’m not going to be releasing the code to the entire program because an ex co-worker of mine and I plan on selling a gallery solution based on this, but here is the code for the folder searching! It is possible to modify this to do a full recursive search, I’ve done it before. Try creating another folder search function, while building the folder path you are passing into the functions as you go. It’s possible to build a recursive output that will show every file in an entire site directory, if you set root_folder = “/”

The Code

root_folder = “/galleries/”
SearchFolders root_folder

Sub SearchFolders(root_folder)
set FileSysObj=CreateObject(“Scripting.FileSystemObject”)
strFullPath = server.mappath(root_folder)
set fldr=FileSysObj.GetFolder(strFullPath)
set FolderList = fldr.SubFolders
For Each FolderIndex in FolderList
Response.Write(FolderIndex.name & ”
“)
Next
set FileList = fldr.Files
For Each FileIndex in FileList
Response.Write(FileIndex.name & ”
“)
Next
End Sub

Sub DisplaySubFolders(root_folder, parent_folder)
set FileSysObj=CreateObject(“Scripting.FileSystemObject”)
strFullPath = server.mappath(root_folder)
set fldr=FileSysObj.GetFolder(strFullPath)
set FolderList = fldr.SubFolders
For Each FolderIndex in FolderList
Response.Write(FolderIndex.name & ”
“)
Next
set FileList = fldr.Files
For Each FileIndex in FileList
Response.Write(FileIndex.name & ”
“)
Next
End Sub

Enjoy,
JJ

API’s Gone Wild

Google, Amazon, Ebay

For all those developers interested in making some seriously interesting or possibly revenue generating programs the google, amazon, and ebay api’s may be a good place to start. I’ve used them in the past always to make something small for a client.

Now it’s my turn, and although the programs I would like to make may be small they’ll be a great learning experience and my own! It’s just that special little feeling of accomplishment when that first XML transaction goes through and you recieve something other than an error code! Ah, yes that’s a good feeling indeed!

So yeah I went a little overboard today and signed up for every major API at the three.
If you’re interested take a look!
Google Maps API (This one is just fun.)
Google Ajax Search API (Might be interesting.)
Google Adwords API (Maybe make some money off this.)
Google Checkout API (Sign up for sandbox if developing. Looks great in my opinion.)
Amazon Web Services (You can make some interesting things out of this!)
Ebay API (Hmmm webservices with ebay… definately could build something interesting!)

Enjoy!

Automating FTP

The Problem

I need to automate an FTP transaction. No big deal right? I need to automate my FTP transaction from inside a .net console program, then execute the same program with multiple instances that process each downloaded file.

Shell()I’m really loving this command. It’s nothing new really but for some reason or another it tends not to be common knowledge. I suppose the land of DOS and those wonderful black and white (although sometimes colorful) programs has been left slowly behind by most newer developers.

The Answer

Technically I already told you the answer to my problem, but I didn’t exactly explain it in full. Here is the code, I’ll explain it more later.

Shell(“ftp -s:ftp.script”)

Isn’t that just great!? It turns out that the normal MS-DOS FTP command already has a basic scripting control in it. So using Shell() to execute ftp, which is in my system PATH, and passing the -s:filename argument into it the ftp. The program then opens your script and uses it to answer each of the prompts it would normally give. My FTP script is very simple and here is a copy.

prompt
open ftp.myserver.com
USERNAME
PASSWORD
cd directory
mget *.txt
bye

Here is a quick breakdown of the script.
prompt: is an FTP program command which disables or enables the interactive prompt
I want this disabled so that I can automatically overwrite files, and accept file downloads.
open: standard open command, set the server IP or server name here
USERNAME: the server always asks for a username – set it here by replacing USERNAME with the real username
PASSWORD: replace it with your password
Repeat your password
cd: change directory – usage: cd /directory/to/work/in/
mget: multiple get – wildcards can be used for filename and extension
Example:
the*developer.txt would match:
thecrazydeveloper.txt, themaddeveloper.txt, and so on.
*.txt matches all files with .txt in the filename
*.* matches all files in the directory
bye: logs you out of the server

Enjoy!