title: creating unique yet ordered ,order number for a customer's order
body: <p>In my <code>java</code> app I need to generate a <code>unique order number</code> for a <code>customer's order</code>.I thought the <code>time of creation of order</code> is a good enough unique value.<code>Two orders cannot be created at the same second</code>.
  To prevent  others from using the ordernumber, by guessing some creationtime value,I appended a part of <code>hash</code> of the creationtime string to it and made it the final order number string.</p>
  
  <p>Is there any unseen pitfall in this approach?Creating the order number based on time of creation was intended to give some sort order for the created orders in the system..
  The code is given here</p>
  
  <pre><code>public static String createOrderNumber(Date orderDate) throws NoSuchAlgorithmException {
          DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
          String datestring = df.format(orderDate).toString();
          System.out.println("datestring="+datestring);
          System.out.println("datestring size="+datestring.length());
          String hash = makeHashString(datestring);//creates SHA1 hash of 16 digits
          System.out.println("hash="+hash);
          System.out.println("hash size="+hash.length());
          int datestringlen = datestring.length();
          String ordernum = datestring+hash.substring(datestringlen,datestringlen+5);
          System.out.println("ordernum size="+ordernum.length());
          return ordernum;
      }
  
      private static String makeHashString(String plain) throws NoSuchAlgorithmException {
          final int MD_PASSWORD_LENGTH = 16;
          final String HASH_ALGORITHM = "SHA1";
          String hash = null;
           try {
                  MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM);
                  md.update(plain.getBytes());
                  BigInteger hashint = new BigInteger(1, md.digest());
                  hash = hashint.toString(MD_PASSWORD_LENGTH);
              } catch (NoSuchAlgorithmException nsae) {
                  throw(nsae);
              }
          return hash;
      }
  </code></pre>
  
  <p>A sample output is</p>
  
  <pre><code>datestring=20110924103251
  datestring size=14
  hash=a9bcd51fc69d9225c5d96061d9c8628137df14e0
  hash size=40
  ordernum size=19
  ordernum=2011092410325125c5d
  </code></pre>