CommsCentral

Adventures in Technology and the Outdoors

Hi and welcome to my site.
This site is broken into 2 parts.

The Tech Journal which is my adventures in technology and is the most active part of the site.

The other half is the Outdoor Journal which is all about my outdoors hobbies.

This main page will always show the most recent post from each half of the site. Click the links above to see more posts from the Tech or Outdoor halves of the site

Most Recent Posts


Posted In: Tech

OpenPGP.js how to encrypt files

Posted at: 2018-02-03 @ 07:51:39

Quick post today on using the openpgp.js library to create an encrypted file that is PGP/GPG compatible.
This was another one of those things that took me longer to implement than I expected!

I write most of my javascript using typescript these days, so that's what I'm showing in my examples. If you want raw JS, see my GitHub or tsc (type script compiler) the example code.

First, let us look at the encryption.
I'm going to supply the key in a format called ASCII armour .
In this format a public key will start with -----BEGIN PGP PUBLIC KEY BLOCK-----

When using this format, pulling the key in requires using the readArmored method:

const publicKey: string = '-----BEGIN PGP PUBLIC KEY BLOCK-----.......';

openpgp.initWorker({}); // initialise openpgpjs
const openpgpPublicKey = openpgp.key.readArmored(publicKey);


Ok now we need to deal with a file. Unfortunately openpgp.js only supports files in Uint8Array, but fs.readFile by default will return a node buffer. Thankfully this isn't that hard!

const file = fs.readFileSync('/tmp/file-to-be-encryped');
const fileForOpenpgpjs = new Uint8Array(file);


Next, we build the request object for openpgp.js. Note, how we actually reference the .keys item attached to the openpgpPublicKey object response of readArmored key

const options = {
data: fileForOpenpgpjs,
publicKeys: openpgpPublicKey.keys,
armor: false
};

The armor: false - setting tells openpgp js not to ascii armor the file output. Hence, We will get a binary object NOT a string. openpgp does support a full file in ASCII armour, I'm unsure if that would be a good idea for a file of any significant size.

Setup complete, now we perform the actual encryption!

const encryptionResponse = await openpgp.encrypt(options); // note the await here - this is async operation


This performs the encryption, now we need to get the file object out of the system, that requires calling message.packets.write()

const encryptedFile = encryptionResponse.message.packets.write();


Right now we have a constant, encryptedFile that is of type Uint8Array. Lucky for us fs.writeFile supports Uint8Array as an input! So we simply write it back to disk!

fs.writeFileSync('/tmp/file-encryped', encryptedFile);


Phew ok, encryption done.

Guess we'd better be able to decrypt, starts much the same.
As private keys can have many sub keys we have to pick one. Hence .keys[0] on readArmored

There is another new thing, .decrypt. Private pgp keys are encrypted, so we have to perform a decrypt using our private key password.
Lastly, we do the cast from buffer to Uint8Array again on our file read

const privateKey: string = '-----BEGIN PGP PRIVATE KEY BLOCK-----.......';
openpgp.initWorker({});

const openpgpPrivateKeyObject = openpgp.key.readArmored(privateKey).keys[0];
openpgpPrivateKeyObject.decrypt('PRIVATE KEY PASSWORD');

const file = fs.readFileSync('/tmp/file-encryped');
const fileForOpenpgpjs = new Uint8Array(file);



Next is our openpgp request options. Little different to encrypt which took me ages to figure out!

const options = {
privateKey: openpgpPrivateKeyObject,
message: openpgp.message.read(fileForOpenpgpjs),
format: 'binary'
};

Fairly obvious now you see it, right?.. The format binary is important telling openpgp.js that we are dealing with a binary, not a ASCII armoured message.


Now we call the actual decrypt. Again this is an async function, so I'm using await here and the raw binary file will be in the .data attribute contains the Uint8Array that is our file.
This we can write directly back to disk as fs supports Uint8Array as an input.

const decryptionResponse = await openpgp.decrypt(options);
const decryptedFile = decryptionResponse.data
fs.writeFileSync('/tmp/file-encryped-then-decrypted', decryptedFile);


Last but not least, it is probably a good idea to include the following at the start of your file, these operations prevent openpgp.js printing version and comment information into any files.

openpgp.config.show_version = false;
openpgp.config.show_comment = false;


Once you have seen it and had it explained it is not that hard. I ended up working this out by reading the openpgp.js unit test code... Not ideal really, so I hope this helps someone out there!

One word of warning. As we're reading files into buffers here and not handling them as streams (I don't think openpgp.js supports streams) there is a risk we could run the system out of memory on a file larger than available memory. I'd already check input file size vs available memory if you can.

All the source is available at https://github.com/adcreare/nodejs-demos/tree/master/openpgp.js



Posted In: Outdoors

Bushwalking - Blue Mountains, out to Ruined Castle from Scenic Railway

Posted at: 2008-09-23 @ 21:44:09

We had wonderful weather for our walk, not a cloud in the sky but also not too hot. Almost prefect walking weather.

I had arranged to meet everyone at the Maple Grove car park, which is just back from the Scenic Railway and normally doesn't have many cars in it. It's also close to the end of the Furber steps which was the planned walk out route.

meeting point


From the car park we walked along the Prince Henry cliff walk to Echo Point and then onto the start of The Giant Staircase. This was probably a couple of KMs and took us around 30 minutes to complete. The descent down the Giant Staircase was straight forward and done without incident while enjoying some of the great views over the Jamison valley.
Jamison Valley

The Giant Staircase sounds worse than it really is. Yes it's steep and narrow, but it's not that long. The descent for us took around 20 minutes and that was with a lot of traffic. The climb back up would take longer. I'd allow 45 minutes to an hour. Less if you're really fit!

giant staircase


Once at the bottom of the stairs we started out for the Scenic Railway which is around 3KM from the base of the Giant Staircase.
We stopped for lunch in a beautiful spot at the base of the Katoomba falls. A short distance before the base of the Scenic Railway. We even celebrated the birthday of one of the group members with cake and all! Much to his surprise.

Waterfall Jamison valley


From the base of the Scenic Railway we walked along the new(ish) boardwalk until the dirt track turn off to Ruined Castle, Golden Stairs and Landslide.

The first part of the track before the Landslide is straight forward. However, when the main fork is reached the lower one needs to be taken or you'll end up against the rock face near Malita Point. The track also cut directly through one of the coal seams in the area. The Scenic Railway was in fact built to service the mines down in the valley long before it became the tourist attraction it is now. When the mines closed up, the railway remained and continues to operate to this day. The base of the railway now has lots of exhibits and signs talking about the old mining days. If you've never been down that way before its certainly worth a look.

I also found what looks like an old mine air shaft between the Landslide and the Golden Stairs. I grabbed the torch and had a quick look in the entrance and found the cutting was almost a prefect rectangle continuing dead straight into the rock for as far as I could see. I would love to know more details if anyone out there has some knowledge about what it is and what shafts in the mountain it connects to.

I should add here just in case someone is dumb enough to get silly ideas. Mine shafts are not something that should be explored. They can be highly unstable and a real death trap for the inexperienced.

Next we came to the Landslide, which I have to say isn't the nicest of walking. Its a lot of small loose rocks which is all too easy to slip on. The path over the rock pile is sign posted with arrows and yellow panted stakes in the ground however inexperienced walkers do need to be careful, its easy to miss a few.

landslide


Once we were past the landslide its mostly flat walk, past the bottom of the Golden Stairs until the turn off to Ruined Castle. The turn off is signposted with the other track continuing on to Mt Solitary. The track up to Ruined Castle was a bit of a killer! It's got a serious climb that lasts for probably 1KM. Once past this it's mostly flat across the ridge to the castle.
From the bottom of the rocks which make up the Ruined Castle, is a short climb to the top of the rocks for the view.

Ruined Castle itself is well worth the walk, the 360 degree views over the Jamison valley are a real treat.

ruined castle



ruined castle



ruined castle


The walk back was uneventful aside from us all starting to get tired and that Landslide rock pile seeming harder on the way back! Once back at the scenic railway, we walked back up the Furber steps to the car park as we'd missed the last train out. It was starting to get dark around this time (about 6PM) but there was enough light to see us out of the valley, although we did have torches just in case we got caught. We got out of the valley, three very tired and sore walkers!

All up, we covered somewhere between 20 and 24KMs in around 6 hours which I think isn't bad going for one day!

I highly recommend the walk, although you may want to cut the distance down a bit by skipping the Giant Staircase and getting the Scenic Railway down, unless you're after a real workout. However, unless you have at least one experienced leader in the group, I wouldn't recommend the walk as it is not a walk to be taken lightly.
The Blue Mountains website grades the walk as for experienced walkers only and says the following:

"Experienced walkers only - High level of fitness and navigational skill required, minimum 3 in group. Advise friends or police of route and destination times".

I don't really agree with the navigational part unless they class reading signs as that. Either way, treat the walk with respect and make sure you have plenty of water as there isn't any to be found that's safe to drink on the walk itself.

Caveman





© 2015 CommsCentral