Android Key Create createRandomKey(int size)

Here you can find the source of createRandomKey(int size)

Description

create Random Key

Declaration

public static String createRandomKey(int size) 

Method Source Code

//package com.java2s;

import java.util.Random;

public class Main {
    private static final char[] HEX_DIGITS = "0123456789ABCDEF"
            .toCharArray();/*from w  w w .ja  v a 2 s.co m*/

    public static String createRandomKey(int size) {
        char[] c = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
        Random random = new Random(); 
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size; i++) {
            sb.append(c[Math.abs(random.nextInt()) % c.length]);
        }
        return sb.toString();
    }

    public static final String toString(byte[] ba) {
        return toString(ba, 0, ba.length);
    }

    public static final String toString(byte[] ba, int offset, int length) {
        char[] buf = new char[length * 2];
        for (int i = 0, j = 0, k; i < length;) {
            k = ba[offset + i++];
            buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
            buf[j++] = HEX_DIGITS[k & 0x0F];
        }
        return new String(buf);
    }
}

Related

  1. generateKey(String key)
  2. generateKey(byte[] randomSeed)
  3. getRawKey(byte[] seed)
  4. getRawKey(byte[] seed)
  5. generateKey(byte[] seed)
  6. generateKey(char[] passphraseOrPin, byte[] salt)
  7. generateKey(char[] passphraseOrPin, byte[] salt)
  8. generateMacSecret()