code: private int calculateScratchCode(byte[] scratchCodeBuffer)
      {
          if (scratchCodeBuffer.length < BYTES_PER_SCRATCH_CODE)
          {
              throw new IllegalArgumentException(
                      String.format(
                              "The provided random byte buffer is too small: %d.",
                              scratchCodeBuffer.length));
          }
  
          int scratchCode = 0;
  
          for (int i = 0; i < BYTES_PER_SCRATCH_CODE; ++i)
          {
              scratchCode = (scratchCode << 8) + (scratchCodeBuffer[i] & 0xff);
          }
  
          scratchCode = (scratchCode & 0x7FFFFFFF) % SCRATCH_CODE_MODULUS;
  
          // Accept the scratch code only if it has exactly
          // SCRATCH_CODE_LENGTH digits.
          if (validateScratchCode(scratchCode))
          {
              return scratchCode;
          }
          else
          {
              return SCRATCH_CODE_INVALID;
          }
      }
docs: This method calculates a scratch code from a random byte buffer of
  suitable size <code>#BYTES_PER_SCRATCH_CODE</code>.
  
  @param scratchCodeBuffer a random byte buffer whose minimum size is
  <code>#BYTES_PER_SCRATCH_CODE</code>.
  @return the scratch code.