Multiplication in Python ― without actually multiplying!

January 31st, 2010

This is just something interesting I wrote last year. It will multiply two integers using an algorithm which is sometimes called “russian peasant multiplication”.

def peasant_multiply(a, b):
    '''Multiply the floor of 'a' and 'b' using only addition, bitshifts, and the binary AND operator.'''
    a = int(a)
    b = int(b)
    product = 0
    while a > 0:
        if a & 1:
            product = product + b
        a = a >> 1
        b = b << 1
    return product

DiceGen ― Automatic generation of Diceware- passphrases

August 10th, 2009

Diceware is a system for generating secure, word-based passphrases by using dice rolls as a source of randomness. There comes a point however, when you realize that enemy intelligence isn’t actually trying to break your encryption, and all you want is to make a strong and memorable passphrase without the fuss of rolling around small cubes.

That’s where DiceGen comes in. DiceGen is a command line program that does all the work for you. Run dicegen.py and it spits out a five word Diceware passphrase.

DiceGen is flexible: by default it uses the original Diceware wordlist to make passphrases, but it can also take simple, one-word-per-line (newline delimited) wordlists―not just Diceware wordlists. To do this, use --word-list-format=simple and --word-list-file=FILE to tell dicegen to use the wordlist at file FILE.

Download

Instructions

DiceGen is very easy to use. To generate five passphrases with ten words each, run python dicegen.py -n5 -w10. Running python dicegen.py --help gives you some more detailed usage information:

Usage: dicegen [-n] [-w]

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -n NUM, --number=NUM  number of passphrases to generate [default: 1]
  -w NUM, --words=NUM   number of words to use in passphrase [default: 5]
  --no-spaces           do not add spaces between words
  --word-list-file=FILE
                        location of a complete Diceware wordlist [default: /path/to/diceware.wordlist.asc]
  --word-list-format=FORMAT
                        how the wordlist is formatted [possible values:
                        diceware, simple] [default: diceware]

Caveots

DiceGen uses python’s random library, which uses the Mersenne twister generator. This generator is not cryptographically secure. Don’t use DiceGen if you happen to be enemies with the NSA or really, really smart people.

Touching up a breakaway wand

August 1st, 2008

Almost any magician who uses a wand and some comedy bits is familiar with a breakaway wand. Certain plastic breakaway wands will develop unsightly chipped edges. Even though these chips are small, they stress the plastic and turn those edges into a color that is significantly more white than the desired black. The result of this wear are visible rings on the wand.

There is however, a very easy and effective touch-up. Grab a black permanent marker and run the tip across all of the chipped edges on your wand and then… Alakazam! Your wand has been returned to a near-new looking status.

File carving OpenDocument text files (.odt)

July 4th, 2008

I recently destroyed (on accident, of course) someone’s file-system. Big time bummer. I needed to recover some files that were made with OpenOffice. The file carving route made the most sense, so I used Bless to inspect a collection of OpenOffice files I had around and made these definitions to work with Foremost and Scalpel. They worked like a charm! Here they are for your benefit:

#---------------------------------------------------------------------
# OPENOFFICE FILES
#---------------------------------------------------------------------
	odt	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.textPK	META-INF/manifest.xmlPK????????????????????
	ods	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.spreadsheetPK	META-INF/manifest.xmlPK????????????????????
	odp	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.presentationPK	META-INF/manifest.xmlPK????????????????????
	odg	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.graphicsPK	META-INF/manifest.xmlPK????????????????????
	odc	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.chartPK	META-INF/manifest.xmlPK????????????????????
	odf	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.formulaPK	META-INF/manifest.xmlPK????????????????????
	odi	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.imagePK	META-INF/manifest.xmlPK????????????????????
	odm	y	10000000	PK????????????????????????????mimetypeapplication/vnd.oasis.opendocument.text-masterPK	META-INF/manifest.xmlPK????????????????????
	sxw	y	10000000	PK????????????????????????????mimetypeapplication/vnd.sun.xml.writerPK	META-INF/manifest.xmlPK????????????????????

XML Character Entities Cheat Sheet

September 23rd, 2007

That key on your keyboard that you probably use for quotations and apostrophes isn’t actually a quotation or an apostrophe key! It’s a remnant from the typewriter era where typewriters had only a small subset of characters that were normally available in a printer’s type case. With computers and the era of desktop publishing, we’ve gone far beyond typewriters and can now be our own typesetters—you just have to know when and how to use the right characters. And trust me, your pages will look a lot spicier and much more professional when you get these down.

When writing your XML/XHTML documents you’ll want to type in these codes to get the character you want to display properly. These are called XML decimal character entities. It’s quite a mouthful, but I’ve found that they have greater support in the wild when compared to the named entities you might be familiar with such as &#ldquo;. Keep in mind that a lot of the especially tricky characters outlined below have important rules. Don’t be intimidated, just do it! Using the proper characters will spice up your documents!

Here’s a helpful little reference that I use all the time!

[—] em dash (&#8212;)
• breaks in thought
• to enclose a clause like with parentheses
• open ranges
• century vague years
• two em dashes for missing letters
• three em dashes for missing words

[–] en dash (&#8211;)
• closed numerical ranges
• indicating a connection
• showing joint authors
• compounding a hyphenation with an adjective

[‒] figure dash (&#8210;)
• linking numbers together which are not a range
• telephone numbers

[−] minus sign (&#8722;)
• subtraction

[‐] hyphen (&#8208;)
• joining compound words

[“] left double quotation mark (&#8220;)
• exact quotations

[”] right double quotation mark (&#8221;)
• exact quotations

[‘] left single quotation mark (&#8216;)

[’] right single quotation mark (&#8217;)
• preferred character for use as an apostrophe

[…] ellipsis (&#8230;)
• before periods for one or more missing words
• after periods for one or more missing sentences
• with no periods for trailing thought

A lot of this information came from a great A List Apart article titled “The Trouble With EM ’n EN (and Other Shady Characters)” by Peter Sheerin as well as from a Wikipedia page on Dashes.

Solution to error adding Sensors Applet

September 9th, 2007

I’m running Fedora 7 with the proprietary Nvidia drivers. After an upgrade to the Sensors Applet, I found that I could no longer add it to my panel. It gave me this error:

The panel encountered a problem while loading “OAFIID:SensorsApplet”.

Little did I know that an awesome new feature had been added and broke the applet. This feature was temperature monitoring for Nvidia graphics cards! When I was trying to add the applet to the panel, the sensor applet attempted to find out my GPU temperature, but failed because of a missing package. By installing the “libXNVCTRL” package, I was able to give the sensors applet the API it wanted and get things working just fine.
Read the rest of this entry »