1

My company develops products, primarily using Matlab scripts, that gets used by our customer in a RedHat environment we do not have access to.

I am trying to figure out a way to to a unix cksum command in Matlab so that when our costumer receives it they can verify that nothing has changed.

Edit for clarification: Since the tools used for development are in matlab, the PM would like the Unix cksum function integrated into the creation so that the output of our tool always matches what we deliver to the customer on a Unix system. We've found that a DOS cksum command and a Unix cksum don't give the exact same results

Jessica
  • 11
  • 5
  • If you want MATLAB to execute `cksum` on the Unix machine, the [`system`](https://es.mathworks.com/help/matlab/ref/system.html) function will allow you to use the terminal utilities available on that machine. – codeaviator Dec 07 '17 at 17:59
  • @codeaviator it looks like a previous comment was deleted suggesting the same >> system('man cksum') 'man' is not recognized as an internal or external command, operable program or batch file. ans = 1 >> >> system('cksum') 'cksum' is not recognized as an internal or external command, operable program or batch file. ans = 1 >> – Jessica Dec 07 '17 at 18:02
  • Yes, I deleted it because I'm not sure that I correctly understand the problem you need to address. Where do you need to run `cksum`? On the Windows machine or on the customer's Unix machine? – codeaviator Dec 07 '17 at 18:05
  • We need to run it on our Windows machine so that the customer can verify it on their machine. – Jessica Dec 07 '17 at 18:06
  • Ok Jessica, but why do you need to do a checksum from MATLAB? You can use the `cmd` or `powershell` utilities available on our Windows machine. Can you provide more details? – codeaviator Dec 07 '17 at 18:10
  • Since the tools are created in matlab the PM wants the cksum to be integrated into the matlab as part of the final output. Additionally, we've found that windows cksums and linux cksums don't always match across systems – Jessica Dec 07 '17 at 18:12
  • You should have a look at [this](https://stackoverflow.com/q/12276426/3599179). – codeaviator Dec 07 '17 at 18:21

1 Answers1

0

So, if your goal is to calculate a *NIX style checksum within the Matlab application itself and you really can't avoid this (for example, calling cksum with unix), the best option you have is to use Java interop within Matlab to achieve this task. Implementing a CRC32 Checksum calculation based exclusively on Matlab built-in functions risks to become a painful task... fortunately, Matlab allows full usage of Java technology within its environment.

The following code should perfectly replicate the *NIX cksum command output:

function crc32 = CalculateCRC32(file_path)

    persistent array;

    if isempty(array) 
        array = javaArray('java.lang.String',0);
    end

    import('java.lang.Long');
    import('java.nio.file.Files');
    import('java.nio.file.Paths');
    import('java.util.zip.CRC32');

    path = Paths.get(file_path,array);
    data = Files.readAllBytes(path);

    provider = java.util.zip.CRC32();
    provider.update(data);

    crc32 = char(Long.toHexString(provider.getValue()));

end

Use it within your application as follows:

crc32 = CalculateCRC32('C:\Path\To\MyFile.something');
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98