Class SipHash

  • All Implemented Interfaces:
    Mac
    Direct Known Subclasses:
    SipHash128

    public class SipHash
    extends java.lang.Object
    implements Mac
    Implementation of SipHash as specified in "SipHash: a fast short-input PRF", by Jean-Philippe Aumasson and Daniel J. Bernstein (https://131002.net/siphash/siphash.pdf).

    "SipHash is a family of PRFs SipHash-c-d where the integer parameters c and d are the number of compression rounds and the number of finalization rounds. A compression round is identical to a finalization round and this round function is called SipRound. Given a 128-bit key k and a (possibly empty) byte string m, SipHash-c-d returns a 64-bit value..."

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int c  
      protected int d  
      protected long k0  
      protected long k1  
      protected long m  
      protected long v0  
      protected long v1  
      protected long v2  
      protected long v3  
      protected int wordCount  
      protected int wordPos  
    • Constructor Summary

      Constructors 
      Constructor Description
      SipHash()
      SipHash-2-4
      SipHash​(int c, int d)
      SipHash-c-d
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void applySipRounds​(int n)  
      long doFinal()  
      int doFinal​(byte[] out, int outOff)
      Compute the final stage of the MAC writing the output to the out parameter.
      java.lang.String getAlgorithmName()
      Return the name of the algorithm the MAC implements.
      int getMacSize()
      Return the block size for this MAC (in bytes).
      void init​(CipherParameters params)
      Initialise the MAC.
      protected void processMessageWord()  
      void reset()
      Reset the MAC.
      protected static long rotateLeft​(long x, int n)  
      void update​(byte input)
      add a single byte to the mac for processing.
      void update​(byte[] input, int offset, int length)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • c

        protected final int c
      • d

        protected final int d
      • k0

        protected long k0
      • k1

        protected long k1
      • v0

        protected long v0
      • v1

        protected long v1
      • v2

        protected long v2
      • v3

        protected long v3
      • m

        protected long m
      • wordPos

        protected int wordPos
      • wordCount

        protected int wordCount
    • Constructor Detail

      • SipHash

        public SipHash()
        SipHash-2-4
      • SipHash

        public SipHash​(int c,
                       int d)
        SipHash-c-d
        Parameters:
        c - the number of compression rounds
        d - the number of finalization rounds
    • Method Detail

      • getAlgorithmName

        public java.lang.String getAlgorithmName()
        Description copied from interface: Mac
        Return the name of the algorithm the MAC implements.
        Specified by:
        getAlgorithmName in interface Mac
        Returns:
        the name of the algorithm the MAC implements.
      • getMacSize

        public int getMacSize()
        Description copied from interface: Mac
        Return the block size for this MAC (in bytes).
        Specified by:
        getMacSize in interface Mac
        Returns:
        the block size for this MAC in bytes.
      • init

        public void init​(CipherParameters params)
                  throws java.lang.IllegalArgumentException
        Description copied from interface: Mac
        Initialise the MAC.
        Specified by:
        init in interface Mac
        Parameters:
        params - the key and other data required by the MAC.
        Throws:
        java.lang.IllegalArgumentException - if the params argument is inappropriate.
      • update

        public void update​(byte input)
                    throws java.lang.IllegalStateException
        Description copied from interface: Mac
        add a single byte to the mac for processing.
        Specified by:
        update in interface Mac
        Parameters:
        input - the byte to be processed.
        Throws:
        java.lang.IllegalStateException - if the MAC is not initialised.
      • update

        public void update​(byte[] input,
                           int offset,
                           int length)
                    throws DataLengthException,
                           java.lang.IllegalStateException
        Specified by:
        update in interface Mac
        Parameters:
        input - the array containing the input.
        offset - the index in the array the data begins at.
        length - the length of the input starting at inOff.
        Throws:
        DataLengthException - if there isn't enough data in in.
        java.lang.IllegalStateException - if the MAC is not initialised.
      • doFinal

        public int doFinal​(byte[] out,
                           int outOff)
                    throws DataLengthException,
                           java.lang.IllegalStateException
        Description copied from interface: Mac
        Compute the final stage of the MAC writing the output to the out parameter.

        doFinal leaves the MAC in the same state it was after the last init.

        Specified by:
        doFinal in interface Mac
        Parameters:
        out - the array the MAC is to be output to.
        outOff - the offset into the out buffer the output is to start at.
        Throws:
        DataLengthException - if there isn't enough space in out.
        java.lang.IllegalStateException - if the MAC is not initialised.
      • reset

        public void reset()
        Description copied from interface: Mac
        Reset the MAC. At the end of resetting the MAC should be in the in the same state it was after the last init (if there was one).
        Specified by:
        reset in interface Mac
      • processMessageWord

        protected void processMessageWord()
      • applySipRounds

        protected void applySipRounds​(int n)
      • rotateLeft

        protected static long rotateLeft​(long x,
                                         int n)