Java ID Value Create createID()

Here you can find the source of createID()

Description

Create a randomly generated string conforming to the xsd:ID datatype.

License

Open Source License

Return

the randomly generated string

Declaration

public static String createID() 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    private static Random random = new Random();
    private static final char[] charMapping = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p' };

    /**/*from   w w  w.  j a  v  a2 s .c  om*/
     * Create a randomly generated string conforming to the xsd:ID datatype.
     * containing 160 bits of non-cryptographically strong pseudo-randomness, as
     * suggested by SAML 2.0 core 1.2.3. This will also apply to version 1.1
     * 
     * @return the randomly generated string
     */
    public static String createID() {
        byte[] bytes = new byte[20]; // 160 bits
        random.nextBytes(bytes);

        char[] chars = new char[40];

        for (int i = 0; i < bytes.length; i++) {
            int left = (bytes[i] >> 4) & 0x0f;
            int right = bytes[i] & 0x0f;
            chars[i * 2] = charMapping[left];
            chars[i * 2 + 1] = charMapping[right];
        }

        return String.valueOf(chars);
    }
}

Related

  1. createID()
  2. generateId()
  3. generateId()
  4. generateId()
  5. generateId()