How far does Gandalf fall when fighting the Balrog in Khazad Dum?

In the extended edition of Fellowship of the Ring Gandalf fights the Balrog and then falls for about 70 seconds.

It takes about 12 seconds to reach terminal velocity: 0.5 * g * t^2 = 0.5 * 9.81 * 12^2 ~ 706m

The remaining 58 seconds Gandalf is at terminal velocity (not speeding up) of 53m/s which is v * t = 53 * 58 ~ 3074m. For a total of 3780 meters, or about 2.35 miles.

Pretty impressive free-fall considering they were ALREADY under ground.

Tags:
Leave A Reply

Perl: biski64 and sfc64 PRNG

Biski64 is a new PRNG there is extremely fast and also very simple. This one was pretty simple so I ported it to Perl.

SFC64 is also largely considered on of the better PRNGs so I ported it also in sfc64.pl.

Tags:
Leave A Reply

PRNG: PCG64 in Perl

I found a PCG64 implementation that I was able to borrow and convert to pure Perl:

# PCG64 RXS-M-XS "simple" variant
#my $seeds = [12, 34];
#my $rand  = pcg64_perl($seeds);
sub pcg64_perl {
    my $seeds = $_[0];
    my $ret   = (($seeds->[0] >> (($seeds->[0] >> 59) + 5)) ^ $seeds->[0]);

    use integer;
    $ret *= 12605985483714917081;
    $seeds->[0] = $seeds->[0] * 6364136223846793005 + $seeds->[1];
    no integer;

    $ret = ($ret >> 43) ^ $ret;

    return $ret;
}

Update: Bonus points because it's really fast:

*               Rate xosh256**  biski64     sfc64     PCG32 Splitmix64     PCG64
xosh256**  1589825/s        --     -26%      -48%      -52%       -58%      -65%
biski64    2145923/s       35%       --      -30%      -35%       -43%      -53%
sfc64      3058104/s       92%      43%        --       -8%       -19%      -33%
PCG32      3311258/s      108%      54%        8%        --       -13%      -28%
Splitmix64 3787879/s      138%      77%       24%       14%         --      -17%
PCG64      4587156/s      189%     114%       50%       39%        21%        --
Tags:
Leave A Reply

PHP: Serve large audio/video files outside of the webroot

I have some large audio files that I need to serve via HTTPs. They need to be protected by my PHP login system so they cannot live inside of the webroot. Fortunately there is a nifty Apache module named X-sendfile, that lets you serve files outside of the webroot.

Credential checking is done in PHP, and then you set a special HTTP header that Apache watches for. When Apaches sees the X-Sendfile header it serves the file directly. This gets you all the advantages of a full-blown HTTP server handling your files, but the convenience and simplicity of PHP for handling authentication.

if ($authorized) {
    $mime_type = mime_from_filename($filepath);

    header("Content-Type: $mime_type");
    header("X-Sendfile: $filepath");
    exit;
}

See also: Get mime type for file

Tags:
Leave A Reply

PHP: Get the mime type for a file

If you need to get the mime type for a file in PHP you can use this function which wrappers the finfo_open() function.

function mime_from_filename($filename) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime  = finfo_file($finfo, $filename);
    finfo_close($finfo);

    return $mine;
}

Note: This function requires the file to exist on the disk.

Tags:
Leave A Reply

Perl: Get terminal width

If you need a fast and simple way to get the terminal width in Perl core use Term::ReadKey. This is almost certainly faster and better than shelling out to tput cols.

sub get_terminal_width {
    my @x      = Term::ReadKey::GetTerminalSize();
    my $width  = $x[0] || 0;

    return $width;
}
Tags:
Leave A Reply

NIST Password Recommedations for 2025

Current NIST Password Requirements for 2025 (SP800-63b).

What’s gone:

❌ Required uppercase, numbers, and symbols
❌ Mandatory password resets every 90 days
❌ Arbitrary complexity policies

What’s required now:

✅ Minimum 8-character passwords (15+ for privileged accounts)
✅ Password screening against compromised credential databases
✅ Support for passwordless authentication and passkeys

Minimum Password Length Requirements

Password length serves as the cornerstone of NIST's updated authentication framework. While the baseline requirement mandates a minimum of 8 characters, security research reveals that passwords under 8 characters can be cracked within hours using modern computing power.

StrongDM has a good summary.

Tags:
Leave A Reply

Two more PRNGs implemented in Perl

In my ongoing quest to implement PRNGs in pure Perl I'm adding the Marsaglia multiply-with-carry family to my collection. These were a little difficult because the math is all done in 128bits which requires a special emulation layer in Perl. The Perl version is much slower than the C version, but that's to be expected without native 128bit variable types.

I also implemented squares64 PRNG which was a little more straight forward. I learned that bitwise shifts are normally unsigned, but if we're in a use integer block bitwise shifts change to signed. This is not what we want so I ended doing a lot of use integer followed almost immediately by a no integer to make sure we're doing the math in the correct mode.

Tags:
Leave A Reply

I was mentioned in the Perl v5.42 release notes

For the fourth year in a row I made it into the official Perl release notes. People get mentioned if they contribute code to the language. To be fair my contributions were documentation updates, but it's still pretty cool to get referenced in the official release announcement that goes out to the entire world.

Tags:
Leave A Reply

Linux: View all the lines in a file after a starting delimiter

I need to print all the lines in a text file after a certain time. You can do this pretty easily with a Perl one-liner:

perl -nE 'if (/13:00/) { $found = 1; next } print if $found' /var/log/messages

The delimiter needs to be in the file to trigger. If there was no line that matches 13:00 nothing will print.

Update: This can also be accomplished with awk:

awk '/13:00/ {found=1; next} found' /var/log/messages
Tags:
Leave A Reply

Perl: Fetch data from a URL

I need a quick way to fetch JSON data from a URL. Using HTTP::Tiny is the easiest way. You can call the function different ways depending on whether you only want the body, or if you want the HTTP status code as well:

my $json = get_url("https://domain.com/stats.json");

my ($html, $http_code) = get_url("https://domain.com/index.html");
sub get_url {
    my $url = shift();

    my $http = HTTP::Tiny->new(verify_SSL => 1, timeout => 5);
    my $resp = $http->get($url);

    my $body   = $resp->{content};
    my $status = $resp->{status};

    my @ret = ($body, $status, $resp);

    return @ret;
}
Tags:
Leave A Reply

PHP: Return a random element from an array

Getting a random element from a PHP array is a multi-step process and I often forget the syntax. Here is a simple function that will return a random element from the provided array.

function random_elem(array $arr) {
    $key = array_rand($arr);
    $ret = $arr[$key];

    return $ret;
}
Tags:
Leave A Reply

Mustache is a very simple and powerful templating language

I am late to the game learning about the Mustache templating language. It is very mature, and has support for all of the languages I care about: Perl, PHP, and Javascript. I am mainly focused on using it in Javascript as it is a great way to format raw data you get via JSON into something more human consumable like HTML.

Mustache.js is incredibly easy to implement and get started with. A single include loads the entire zero-dependency library. As of v4.2.0 the library is tiny, clocking in at only 25k. A big selling point is the ease of installation and use. You can be up and running with Mustache.js in less than 5 minutes.

<script src="/js/mustache.min.js"></script>
var data = { name: "Jason", animal: "Kitten" };
var str  = Mustache.render("<p>Hello {{name}}, I hear you like {{animal}}s</p>", data);

I liked it so much I wrote up a Mustache sandbox to allow me to play around with the templating system live. The template syntax is a little archaic, but it is workable.

Note: They even have Arduino support, which I need to investigate.

Tags:
Leave A Reply

PHP: Simplify sending JSON to a browser

I can never remember the correct MIME type for JSON so I wrote this wrapper function to simplify sending JSON to a browser.

// Convert a PHP data structure to JSON and send it to the browser
function json_output($obj) {
    $output = json_encode($obj, JSON_INVALID_UTF8_SUBSTITUTE);

    // http://tools.ietf.org/html/rfc4627
    header('Content-type: application/json');
    print $output;
    exit;
}
Tags:
Leave A Reply

Perl: Search and replace a string across your entire code base

I need to rename a function anywhere it is found in my code base. This requires going through every file and directory recursively, and checking each file. Using the Linux find command we can find all the files and then hand the search and replace off to Perl. Easy peasy.

find codebase/ -type f -exec perl -pi -E 's/foo/bar/g' {} +

or using fd instead:

fd . codebase/ --type f --exec-batch -pi -E 's/foo/bar/g' {}
Tags:
Leave A Reply