supermerill / SuperSlicer

G-code generator for 3D printers (Prusa, Voron, Creality, etc.)
4.05k stars 516 forks source link

Enable support to generate SHA256 checksum when exporting gcode file #2851

Open shamal69 opened 2 years ago

shamal69 commented 2 years ago

Is your feature request related to a problem? Please describe.

On multiple occassions I have generated valid gcode from SuperSlicer, however when uploading to Klipper, the file becomes corrupted, resulting in invalid gcode being actioned by the printer. For small prints, I can live with reprinting, however on large prints (4hrs+) when this occurs nearing the end of the print, substantial time and filament is wasted.

Describe the solution you'd like Ability to include SHA256 checksum generation into exported gcode file. Ideally as commented content at the top of the file.

Describe how it would work When the option has been selected to "Enable SHA256 checksum" in the Printer Settings, a commented SHA256 checksum for the exported gcode file is inserted at the top the file.

Describe alternatives you've considered

  1. Running my own SHA256 hash once gcode has been generated, upload to Klipper (Fluidd) and then SHA256 check file on RPi file system prior to commencing print. This is very time consuming and as Fluidd has support for SHA256 checksum verification, it would be a simple solution.

  2. Obtaining a fully shielded USB cable to connect the RPi and the printer.

Additional context I appreciate the consideration of this request. Thank you for the GREAT software!

mmirate commented 2 years ago

In the case of real checksum algorithms, like SHA256, no file can contain its own checksum.

Consider a file whose checksum is not yet determined; it has a particular checksum C1, but no contents which express that its checksum is C1. Now you insert the checksum near the top of the file as requested. The result of this insertion is a slightly different file which therefore has a checksum C2 != C1, but which contains an expression of checksum C1 (not C2).


What I believe would be far more feasible, is to collaborate with the web-API server folks (Moonraker, DWC, OctoPrint, etc.) to have some way to specify the checksum as a parameter to the API call which initiates the upload. Then Moonraker (or DWC or OctoPrint) can verify that checksum for itself, and react accordingly in case of mismatch.


What I believe could be far simpler still, albeit perhaps annoying, is to make the SHA256 checksum available as a substitution parameter in the filename. Then it just takes a very small shell script™ to check any given file, and since Klipper is just Linux under the hood, that's easy to barf up in a few minutes. (Not so easy: (a) integrating that shell script into Klipper so it never prints anything whose filename contains an invalid SHA256 checksum; (b) physically reading from the web UI screen the resulting filenames, which if this feature used, would attain brand-new levels of ludicrous length; and (c) doing any of this on pure-microcontroller systems such as DWC-in-standalone-mode.)


Aside: if all of your errors are incomplete uploads rather than actual garbled data, then you don't actually need any slicer changes. Your use of Klipper already dictates that the g-code's final non-comment line is "END_PRINT", so it should only take a very short shell script to determine whether a g-code file to be printed contains that line or not.

Even firmwares as primitive as Prusa's Marlin fork can - when asked to print a particular file from the SD card - scan to the end of the file, search for the Marlin g-code for "end the print", and if it is not found, halt until the user confirms that that condition was expected. (cf https://help.prusa3d.com/article/file-incomplete-continue-anyway_1847) I am moderately surprised by the lack of such a safety feature in Klipper, and I would be similarly surprised to discover such a thing about Duet.

cyr123 commented 2 years ago

If you wanted to have the SHA embedded in the file it could be solved the same way as embedded checksums and hashes in files and protocols usually are, calculate it first and then add the line at the top (or bottom). To check it, exclude that line from the calculation.

Of course that makes it more complicated to check, you can't just use the standard sha256sum command or similar. It becomes a file format extension, and every tools has to follow the exact same steps when dealing with it.

shamal69 commented 2 years ago

Hi, I have diagnosed the issue and it lies entirely with the SuperSlicer Binary on MacOS (Monterey 12.4). There is some significant work and diagnosis of the issues I was encountering at https://github.com/fluidd-core/fluidd/issues/703 Do you want me to open a new issue to address this? Enabling checksum verification would not have helped it seems - the MacOS version (installed from the pre-compiled binary) inserts random G-Code commands that force the printer off the bed and also randomly inserts non-ascii characters into the exported G-Code file ... it occurs randomly however, so is not reliably reproducible, although MOST of the G-Code generated has an error of some kind - in my setup anyway ... The Windows version is EXCELLENT, so please keep up the great work! Thank you