help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Hash function doesn't work


From: Francesco Potortì
Subject: Re: Hash function doesn't work
Date: Sun, 02 Apr 2017 11:49:00 +0200

This is a <del>hack</del> function I came up with for creating sha1
signatures on Octave 4.0. The problem is that the SHA1 function from the
"general" package is broken on Linux 64 bit.  On Octave 4.2 the problem
is solved by using the hash() core function.

In my application, I need to anonymise a list of MAC numbers gathered on
the air, and I want a one-way 12-byte in, 12-byte out has function, so I
truncate the sha1 hash.

This worked for me, but note that it has not gone serious testing,
because I use it only once each time I get a new log, so I run it few
times on real data, and it was quite short data.

## Convert a cell array of MAC strings to one of hashed strings
## The hash is composed by the last 12 characters of the SHA1 hash
function hashs = mac_to_hash (macs)
  maxretries = 5;
  hashs = cell(size(macs));
  for idx = 1:numel(macs)
    if (isunix)
      while (isempty(hashs{idx}))
        try
          [ifd, ofd, pid] = popen2("sha1sum", {"--binary", "--tag"});
          fputs(ifd, macs{idx});
          fclose(ifd);
          sleep(0.1);
          hashs{idx} = substr(fgets(ofd), -13, 12);
          fclose(ofd);
          waitpid(pid);
        catch
          fclose(ifd); fclose(ofd);
          if (--maxretries > 0)
            warning("Problem using sha1sum, retrying...\n");
            sleep(1);
          else
            error("Too many errors with sha1sum, giving up.\n");
          endif
        end_try_catch
      endwhile
    else
      pkg load general
      hashs{idx} = substr(sprintf("%02x", SHA1(toascii(macs{idx}))), -12);
    endif
  endfor

  ## Consistency check
  [s e] = regexp(hashs, "[0-9a-f]+");
  assert(all(all((cell2mat([s e]) == [1 12]))),
         "Internal error while computing MAC hashes");
endfunction

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it




reply via email to

[Prev in Thread] Current Thread [Next in Thread]