Some Bits: Nelson's log

8 posts in June 2016

Non-cryptographic hashes

 

About once a year I need to hash some data to give it a name / short checksum. I don’t need a cryptographic hash. But I end up using SHA-1 or MD5 anyway because it’s so available. A non-crypto hash should be ~100x faster to process data, which could actually matter. But finding a standard, portable, non-cryptographic hash with optimized implementations for Python, Node, etc is a challenge.

A lot of history of hash functions is producing 32 bit values on 32 bit CPUs. That stuff is mostly obsolete. 32 bit hashes are not useful for many applications; you expect hash collisions after just 2^16 ~= 65,000 objects. And 32 bit CPUs are largely irrelevant now. You really want a micro-optimized machine code implementation for AMD64 that is aware of cache geometry, pipeline optimizations, etc. It may be now that you want a GPU implementation; you certainly do for high throughput crypto hashes. Not clear if it matters as much for non-crypto hashes and deployment realities mean you may not have a GPU at all.

Stock Python has the hashlib library, which includes crypto hashes like MD5 and SHA-1. It’s highly optimized. There’s also CRC32 and Adler32 hiding in the zlib library, but they are only 32 bit hashes and therefore not useful.

MurmurHash3 is the usual recommendation for a non-cryptographic hash. It’s been around a long time and widely implemented. It’s also a bit confusing, with both 32 and 128 bit versions and multiple Python implementations.

There’s a bunch of “nextgen” hash functions out there. Metrohash and xxHash both look promising on first glance.

SMHasher is the standard test and benchmark suite for hash performance. I’d love to read a carefully done comparison of hash functions out there in contemporary languages and deployment environments.

some ffmpeg bash functions

I’m doing a lot of trashy video editing on the command line. I’m taking FLV files from a screen recorder, trimming them to a time frame and converting them to MP4. And then I’m concatenating multiple MP4 files. This is all very confusing to remember how to do in ffmpeg so here are two shell functions I use to do the work for me. There’s nothing particularly clever here except the <() process substitution for the catmp4 program, which creates a temporary file (or named pipe or something) with the output of a command.

function tomp4 {
    ffmpeg \
      -i "$1" \
      -c:v libx264 \
      -preset slower \
      -crf 19 \
      -ss "$2" \
      -to "$3" \
      "${1%.flv}".mp4
}

function catmp4 {
    ffmpeg \
      -f concat \
      -safe 0 \
      -i <(for f in "$@"; do echo "file '$PWD/$f'"; done) \
      -c copy \
      out.mp4
    ls -l out.mp4
}

Ubiquiti cranky customer

Very happy with my Ubiquiti network hardware. Unimpressed with the software experience. Just sent this cranky email to their support, where I’m sure I will get a canned “thanks for the note” reply and nothing will improve.

Your products have three problems that harm the perception of security. Y’all make excellent network hardware, but your software should be as excellent. I’ve already worked around these problems, just registering a complaint.

The Unifi Controller Mac application is not signed. MacOS refuses to launch it until I disable Gatekeeper security.

The Unifi Controller does not come with a valid SSL certificate. I realize this is complicated, but it’s a bad experience to require users to disable Chrome SSL checks to use the product.

Your reset password links from ZenDesk are being flagged as spam by Gmail. They say “Why is this message in Spam? It has a from address in ubnt.com but has failed ubnt.com’s required tests for authentication. Learn more”.

Thanks for listening,
Nelson

Bash arithmetic syntax

For years I’ve been doing bash arithmetic like this

echo $[2+2]

TIL I’m supposed to be doing it like this

echo $((2+2))

Who knew? Everyone, and apparently since 1996 with the release of bash 2.0. The $(()) syntax goes back to the 1988 Korn Shell, which predates when I learned Unix. But I learned most of my fancy shell scripting in the 1989–1994 time frame, when bash was the new hotness and it felt edgy using advanced features unimagined by Bourne (blessed be his name). And I preferred bash to ksh.

I learned this when someone edited one of my StackOverflow karma factories, on how to generate random numbers in bash. I updated the syntax in my answer. (Stack’s moderation review decided it was a bad edit. They were wrong.)

Ubiquiti Unifi access points

Boy, bad first impression of a product. I got a Ubiquiti wireless access point. Figured I could set it up in 10 minutes and go; what could be easier? Lol, joke’s on me. They now require a 200 megabyte “Unifi Controller” running on some machine on your network to configure the devices; no more simple web interface. Instead the controller talks some magic protocol to the devices and then runs its own web server on port 8843 with a fancy web UI. Did I mention the Mac binary is not signed, so it won’t even run without working around Mac’s security system? And the SSL certificate doesn’t work? Awesome.

The software itself seems useful but complex. The manual is 154 pages. It provides group management of all Ubiquiti devices on my network. This seems like it would be great if I had a complicated network with a bunch of Ubiquiti devices. Maybe that’s their market. I just have one device, though, and it better play nice with the rest of my network. Fortunately this is just an AP, and stumbling through I got it working without much effort.

I think it was as simple as going through the basic config of the controller software (username, password, wifi settings), then choosing the Devices tab and clicking “adopt” on the access point.

Sitting 6 feet from the AP I’m getting a full 100Mbps according to DSLReports’ speed test. (Link layer claims 216Mbps). Putting the AP where my old one was gives more like 20Mbps, but I think it’s still better WiFi than the Asus RT-AC66U that I use. But the whole point of this is I want a second AP for upstairs, so I’ll end up running both.

Not a fan of the Power-over-Ethernet brick they provide. I’m confused about POE, there seems to be multiple standards for voltages and which pins are hot. I have no idea if it’s feasible for me to run POE for my whole house, particularly since this punchdown board everything goes through is some Levitron piece of crap that can’t even reliably do gigabit.

Factory Idle

I’ve been hooked on Factory Idle, a well made little puzzle / idle game that runs in the browser. You lay out factories from modular components. It’s basically a casual version of Factorio, with deliberate periods of idleness built in where you let it run in the background and do its thing. Designing factories feels a lot like programming.

Below is a screenshot of my current research factory. I’m at tier 3 of factory research, where I have metal labs and oil&gas labs (left and bottom) both generating reports and feeding them to analytics labs when then deliver them to the research centers on the right. It’s not at all space efficient, as managing flows gets more complicated I’ve been deliberately leaving space to make it easier to design and refactor. The complex piping in the center room is particularly subtle, a system to mix awkward rates like the 11/20 output of the blue metal labs feeding the 32/40 orange inputs of the analytics labs.

(Note this layout has a bug; one of the gas labs in lower left is missing its gas input.)

Screen Shot 2016-06-02 at 12.54.44 PM.png

Moving forward with lolslackbot and Django

I’m very encouraged with how my little Django experiment has worked out. I’m ready to start using it with lolslackbot. A big breakthrough was realizing that I can start using Django as soon as I trust it not to corrupt my database. Keep the existing Python cron job in old code, not using Django, and then have a separate webapp that also uses the same database. The cron job and the webapp will be reading the same tables but in general not writing the same tables. It seems like a good transition plan.

Only now I really have to face the sqlite question. I’m confident that ultimately I need to be using PostgreSQL. I’m envisioning thousands of web users updating the database; there’s no way that works with sqlite’s database-level locking. Even with low traffic the cron job and the webapp will be stepping on each other. I need to switch. But when?

Do I switch databases first, porting my cron job code over to a Postgres-backed system? That seems really painful and not much fun. Lots of backend work with no visible features.

Or do I use Django first, having it talk to the sqlite database? The lock contention won’t be a real problem if I’m the only webapp user. It lets me build fun / useful features sooner. And it gives me some more experience with data modelling. I’m pretty sure I’m going to want to refactor the schema along with the Postgres update, it’d be better to do that after doing some of the webapp work. The risk with Django-first is I do a lot of sqlite-specific work that is ultimately wasted. But Django’s ORM insulates you from the underlying database pretty well, so maybe that doesn’t matter?

Also curious how the Django ORM is going to work for me. I use a couple of non-standard SQL things now. Mostly “insert or replace”, which is sqlite’s upsert-like extension. Does the ORM expose those? Also very curious how testing will work. sqlite is so simple when managing test environments. But I know the Django folks have thought this through.

adding lolslackbot to a new slack

I had to add my bot to a new Slack today (8 new users!) and forogtten how. My bot isn’t a full Slack App, it’s just a bot user, which in Slack terms is a “custom integration” tied to my personal user account for Nelson. Here’s the steps to set it up:

  1. Log into the Slack as Nelson, in the Web UI
  2. The account needs permissions to add custom integrations. You get this by default if you’re an Admin, otherwise it needs to be added under Team Settings / Permissions
  3. In upper left corner, click the down arrow near the Slack name to get to the menu for preferences / etc. Choose the “Apps & Integrations” option under the Slack team’s menu.
  4. Ignore the confusing app directory. Click “Manage” in upper right.
  5. Click Custom Integrations
  6. Click Bots
  7. Click Add Configuration
  8. Fill in the form and save it. Also copy the API token.
  9. I had a problem with a new Slack where I couldn’t add a Bot user, I think the UI acts differently for the very first integration. I managed to create the bot though by hitting this link: https://my.slack.com/services/new/bot
  10. Create a channel for the reports to go to. (Or use an existing one.)
  11. Invite @gamebot to that channel.

That’s the Slack UI process for adding my bot. Kind of a mess, but it’s because I’m using the old bot interface and not the new fancy Slack App integration system that I should be.

After all that setup, then it’s on to Python code to find the channel token. Then set up the destination and subscriptions via code / SQL. All so manual, must build tools.

Speaking of tools I’m pretty convinced Django is the way forward. With a web UI, not some Slack interactive thing.