Making and applying patches

This is actually something quite simple, so simple in fact that there is not much documentation for it. Anyways, to make the patch:

diff -urN file.orig file.modified > file.patch

then to turn a copy of file.orig into file.modified

patch file.orig file.patch

And then file.orig is updated to match file.modified.

Update: Or you could just git.

Alphabetize XFCE desktop icons

A little script that alphabetizes desktop icons in xfce, folders before files, and hides files with a ~ at the end or a . at the beginning (hides hidden files). You can grab it here.

apt-fast with preview, autocompletion and installer script

There were a few things that were preventing me from adopting use of Matt Parnell’s apt-fast wrapper for apt-get.
1) no preview of action – i.e. when I install a package, it downloads all the files before it tells me what it would be doing, if it’s going to install packages I don’t want I don’t want to waste the time downloading the packages.
2) no autocompletion – was the library libexifjpeg62-0-dev or libexifjpeg62.0-dev, now you don’t have to know
3) there was no easy method of installing (not that that was much of an impediment), it was really the first two.

Anyways, I’ve made some modifications and now it supports all 3, that is preview of action before download, autocompletion of both action names and package names, and an installer script. Everything tested on some variant of *buntu. You can find the mods here.

Secure Passwords Made Easy

There are really only a few requirements for passwords:
1) not easily guessed
2) easily remembered (or reproduced given some sort of privileged information)
3) different password for each site

What I’ve done is to make something that uses the username and website, along with a password and/or passfile to generate a random password. This has a few advantages in that,
1) the passwords are very random in the sense that they do not resemble dictionary words, breaking these using a guessing attack would have to use 95^characters guesses. Assuming a 6 character password (and with this tool there is no reason to use anything other than the longest password allowed by the website), and 10^4 guesses per second (which is ridiculously high), it would still take longer than 2 years to guess. At 12 characters it would take 10^12 years, assuming the same rate of guessing, even if we assume one guess per clock cycle (counting hyperthreaded) it would take 400,000 years.
2) it’s super easy to get your password back, you just run the program again, and voila, there it is. Even if someone knew you were using this program, they’d have to guess secret words, and or secret files. Here there’s a slight hole, if they know your username to a given website, and you choose something like password as your key they might try that. If you use a file, a small image would work great. They’d have to essentially know exactly what file you were using.

#!/usr/bin/perl
use strict;
use Digest::SHA;
# some parameters
my $password_length = 8;
my $include_lowers = 1;
my $include_uppers = 1;
my $include_specials = 1;
my $include_digits = 1;

# get conversion strings
my $lowers = "";
my $uppers = "";
my $digits = "";
my $specials = "";
for(my $i = 33; $i < 127; ++$i)
{
if($include_digits && chr($i) =~ m/\d/){$digits .= chr($i);}
elsif($include_uppers && chr($i) =~ m/[A-Z]/){$uppers .= chr($i);}
elsif($include_lowers && chr($i) =~ m/[a-z]/){$lowers .= chr($i);}
elsif($include_specials && chr($i) =~ m/[^A-Za-z\d]/){$specials .= chr($i);}
}
my $all_characters = "${lowers}${uppers}${digits}${specials}";
#print "$all_characters\n";
my @characters_array = split("",$all_characters);

my $secretfile;
# full path to any file you like, could be a picture,
# you cannot edit this file or else it will change your passwords
# This crossed my mind as a good random seed, that would be
# easy to hold on to.

my $secretword;
# a secret "word", can be a number, whatever, I might recommend
# using 10 digits of pi starting at a number you can remember
# address, zipcode, phonenumber... can't be very long, more like a pin
# http://www.angio.net/pi/piquery

# get number of arguments
my $numargs = $#ARGV + 1;
my $username;
my $sitename;
for(my $i = 0; $i < $numargs; ++$i)
{
if($ARGV[$i] =~ m/^-.*/) # if it starts with a dash treat as a flag
{
my $flag = lc($ARGV[$i]);
++$i;
if($ARGV[$i-1] =~ m/.*user.*/){$username = lc($ARGV[$i]);}
elsif($ARGV[$i-1] =~ m/.*help.*/)
{
print "./password_generator [-user username] [-site sitename] [-word secretword] [-file secretfile]\n";
print "secretword or secretfile will be used to seed the randomness.\n";
print "The advantage to this script is that passwords can be regenerated\n";
print "and yet are very difficult to guess.\n";
print "\n";
exit(0);
}
elsif($ARGV[$i-1] =~ m/.*file.*/){$secretfile = $ARGV[$i];}
elsif($ARGV[$i-1] =~ m/.*word.*/){$secretword = $ARGV[$i];}
elsif($ARGV[$i-1] =~ m/.*site.*/){$sitename = lc($ARGV[$i]);}

}
}

# get site name
if(!defined($sitename))
{
print "What is the name of the site for which you need a password?\n";
$sitename = <STDIN>;
chomp($sitename);
$sitename = lc($sitename);
}

if(!defined($username))
{
print "What is the username on the site?\n";
$username = <STDIN>;
chomp($username);
$username = lc($username);
}

if(!defined($secretword) && (!defined($secretfile) || ! -e $secretfile))
{
if(defined($secretfile)){print "Could not find $secretfile"; exit(0);}
else
{
print "You have given neither a secret word nor a seed file (you can use both).\n";
print "Please specify a secret word:\n";
$secretword = <STDIN>;
chomp($secretword);
}
}

my $sha = Digest::SHA->new(512);
print "\n";
print "sitename   = $sitename\n";

if( -e $secretfile )
{
print "secretfile = $secretfile\n";
$sha->addfile($secretfile);
}

if(defined($secretword))
{
print "secretword = $secretword\n";
$sha->add("${secretword}");
}

if(defined($username))
{
print "username   = $username\n";
$sha->add("${username}");
}

print "\n";

$sha->add("${sitename}");
#print substr($sha->clone->hexdigest,0,15)."\n";
my $decdigest = hex(substr($sha->hexdigest,0,15));#cut to 15 to avoid overflows

# random seed using the hash
srand($decdigest);
my $password;
while( ($include_lowers == 1   && &stringcontains($password,$lowers)   == 0) ||
( $include_uppers == 1   && &stringcontains($password,$uppers)   == 0) ||
( $include_digits == 1   && &stringcontains($password,$digits)   == 0) ||
( $include_specials == 1 && &stringcontains($password,$specials) == 0))
{
$password = "";
for(my $iii = 0; $iii < $password_length; $iii++)
{
my $randomnumber = int(rand()*length($all_characters));
$password .= $characters_array[$randomnumber];
}
}

# finally print the password
print "$password\n";
exit(0);

# function to check if string contains at least one of the characters in second string
sub stringcontains
{
my($string, $search_class) = @_;
if($string =~ m/[${search_class}]/){return 1;}
return 0;
}

Rename files with single quotes

So you have a bunch of files that have single quotes in them (silly windows people…). And you want them renamed to reasonable things that you can actually type without resorting to an army of backslashes and quotes. This will eliminate all the single quotes, and most of the other substitutions you might do make much more sense than this, so this should get you going.

rename ‘s/”\’\”//g;’ files

Compressing Large Movies (mov->avi) using mencoder

Getting giant .mov files from your camera, try this to convert the silly format to mp3/h264, which is apparently a pretty awesome format, saving you lots of space and looking pretty good. The downside of course is that this will bring you computer to its knees. Especially if it’s a longer file. It’s all multithreaded so that it takes advantage of all the cores available, which is great otherwise it might take all day.

mencoder -oac mp3lame -ovc x264 INPUT.MOV -o output.avi

rm eating everything, here’s a bit of my solution

Everyone’s who’s worked in a computer environment which assumes you know what you’re doing knows that you don’t in fact always know what you’re doing. So I like a couple of things to be true. First, I want the machine to do what I say when I say it. Second, I want to be able to undo whatever it is I just did when I realize I just deleted a bunch of critical files. My solution to this comes in a few parts. First, I make a trash folder which holds files on the way out. Second, when I say rm, I actually just move those files to the trash folder, which is very fast. Then later, I zip the files in the trash, so while they’re sitting there they don’t take up any space. Now the file is gone from the working directory and most of the space is reclaimed. And if I need the file back I know where to look to get it. I might need to unzip it, but it’s there. And I never need to empty the trash, because every day those files older than 30 days old go buh-bye. So machinery which lets me recover files, doesn’t slow anything down, and requires no maintenance. Check. I might recommend testing everything on the command line before you let if loose.

First, make a trash directory, on ubuntu one is made for you, namely ~/.local/share/Trash/files/ . I put this in my .bashrc

export TRASH=”~/.local/share/Trash/files/”

though of note I use the full path here. I think because of the way we’re going to use bash substitutions it needs to be a full path. I replace rm with my own version, which lives in ~/bin/ which is in the path before the real rm.

#!/usr/bin/perl
# safer rm
use strict;
my $carg;
my $cmd;
my $trash = $ENV{'TRASH'};
if($trash eq "")
{
     print "\$TRASH is empty string. exiting";
     exit(0);
}
for(my $i=0; $i < $#ARGV+1; $i++)
{
     $carg = $ARGV[$i];
     $carg =~ s/\/$//g;#does what?
     if(!(-e $carg)){next;}
     system("touch $carg && /bin/rm -rf $trash/$carg; mv $carg $trash");
}

I make two additions to the crontab:
30 3 * * * empty_trash
*/30 * * * * zip_trash

and then there’s empty_trash and zip_trash:
#!/bin/bash
# empty_trash
if [ "$TRASH" == "" ]; then
     echo '$TRASH is not set, bailing.'
     exit
else
     find $TRASH -xdev -mindepth 1 -mtime +14 -print0|xargs -P 2 -n 10 -0 --no-run-if-empty /bin/rm -r
fi

#!/bin/bash
# zip_trash
if [ "$TRASH" == "" ]; then
     echo '$TRASH is not set, bailing.'
     exit
else
     find $TRASH -mindepth 1 -xdev -type f -print0 |xargs --null -P 7 -n 1 --no-run-if-empty nice -n 19 gzip -f -q 2>&1
fi