How can I protect a .hex file (e.g. a teaching resource) so the source code is not readable?
I want to lock a .hex file so that students can’t see the contents.
I want to give a .hex file to students that demonstrates a target to achieve, but in a way they cannot read the source code.
How can I prevent ‘dragging and dropping’ a .hex file into the PXT editor from viewing the source code?
The PXT editor attempts to embed a compressed version of the original program source code into the .hex file, if there is space for it. The method that is used to compress and embed is described in the links at the end of this article. This is so that if you drag and drop a .hex file back into the pxt.microbit.org editor it will reconstruct your source program for you to allow you to view and change it.
However, sometimes it is useful to be able to ‘lock’ a .hex file, perhaps you have written a program that you want students to download from the web or from your intranet and load it onto the micro:bit to work out what it does, but you don’t want them to be able to ‘cheat’ and drag and drop the .hex file into the pxt.microbit.org editor to view the original source program.
STEPS TO LOCK A FILE
1. Open the .hex file in a text editor such as Notepad on windows, or TextEdit on a Apple Mac. This is an intel-hex format file, the format of which is described in the links below. Basically each line has a short header, and the last two characters of the line are a computed checksum formed by adding up all the data on that line.
2. Search for the string 41140E2FB82FA2BB using the search feature of your text editor. If you can’t find this magic string, try searching for a shorter part of it such as 2FB82FA2BB, as it *might* span multiple lines.
3. Find the ’41’ part and change it to ’40’ – this reduces the total sum of the line by one, so…
4. at the end of that line, look at the last two characters. This is a hex number. Add one to it to make sure that the checksum is now correct.
5. Save the file, then try and drag and drop it into pxt.microbit.org – it should now be impossible for PXT to read it. It will pop up an orange message saying ‘Sorry we could not recognize this file’.
6. Drag and drop the file onto the MICROBIT drive, and make sure your code works.
The attached hex file microbit-secret.hex has been locked, try dragging and dropping it into pxt.microbit.org and you will see that it will not load it.
Here is how we locked that .hex file. First we searched for the magic string – in this case the :10FCC000 before it is part of the memory address information that appears at the start of every intel-hex line.
Then, we changed the 41 to a 40 (thus reducing the sum of the line by one) and added one to the 30 checksum at the end, making it 31.
Finally, we saved this file, then drag and dropped into into PXT to prove it was locked.
Note, if you update the checksum incorrectly, the file will not load properly onto the micro:bit, so make sure you test it on your micro:bit too before distributing it.
When you modify the last two characters of the line to add one to the checksum, this is a hexadecimal number, and it must be updated cyclically. So, here are some examples of how to ‘add one’ to a 2 digit (8 bit) hex number
30 becomes 31
39 becomes 3A
3F becomes 40
FF becomes 00
 Detailed information about the PXT metadata record: https://support.microbit.org/solution/articles/19000044768–hex-file-format
 Intel Hex format: https://en.wikipedia.org/wiki/Intel_HEX