Java ID Value Create generateID(final String message)

Here you can find the source of generateID(final String message)

Description

Generates a sensor id from description and current time as long.

License

Open Source License

Parameter

Parameter Description
message sensor description

Return

generated sensor id as hex SHA-1.

Declaration

public static String generateID(final String message) 

Method Source Code

//package com.java2s;
/**//from w  ww .  java 2s .  co  m
 * Copyright (C) 2013
 * by 52 North Initiative for Geospatial Open Source Software GmbH
 *
 * Contact: Andreas Wytzisk
 * 52 North Initiative for Geospatial Open Source Software GmbH
 * Martin-Luther-King-Weg 24
 * 48155 Muenster, Germany
 * info@52north.org
 *
 * This program is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; even without the implied
 * WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program (see gnu-gpl v2.txt). If not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA or
 * visit the Free Software Foundation web page, http://www.fsf.org.
 */

import java.security.MessageDigest;

import org.joda.time.DateTime;

public class Main {
    /**
     * hexadecimal values
     */
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    /**
     * Message digest for generating single identifier
     */
    private static MessageDigest messageDigest;

    /**
     * Generates a sensor id from description and current time as long.
     * 
     * @param message
     *            sensor description
     * @return generated sensor id as hex SHA-1.
     */
    public static String generateID(final String message) {
        final long autoGeneratredID = new DateTime().getMillis();
        final String concate = message + Long.toString(autoGeneratredID);
        return bytesToHex(messageDigest.digest(concate.getBytes()));
    }

    /**
     * Transforms byte to hex representation
     * 
     * @param b
     *            bytes
     * @return hex
     */
    private static String bytesToHex(final byte[] b) {
        final StringBuilder buf = new StringBuilder();
        for (final byte element : b) {
            buf.append(HEX_DIGITS[(element >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[element & 0x0f]);
        }
        return buf.toString();
    }
}

Related

  1. createID()
  2. generateId()
  3. generateId()
  4. generateId()
  5. generateId()
  6. generateID(int seed)
  7. generateID(String origin)
  8. generateId(String salt)
  9. generateSalt()