Package org.platformlayer.ops.crypto

Source Code of org.platformlayer.ops.crypto.Passwords

package org.platformlayer.ops.crypto;

import java.security.SecureRandom;

import org.platformlayer.core.model.Secret;

public class Passwords {
  final SecureRandom random = new SecureRandom();

  public static final char[] ALPHANUMERIC_CASE_SENSITIVE;
  public static final char[] ALPHANUMERIC_CASE_INSENSITIVE;

  static {
    ALPHANUMERIC_CASE_INSENSITIVE = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
    ALPHANUMERIC_CASE_SENSITIVE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
  }

  public Secret generateRandomPassword(int length) {
    return generateRandomPassword(length, ALPHANUMERIC_CASE_SENSITIVE);
  }

  public synchronized Secret generateRandomPassword(int length, char[] universe) {
    char[] password = new char[length];
    for (int i = 0; i < length; i++) {
      password[i] = universe[random.nextInt(universe.length)];
    }
    return Secret.build(new String(password));
  }

  public Secret generateIpsecPSK() {
    while (true) {
      Secret s = generateRandomPassword(64, ALPHANUMERIC_CASE_SENSITIVE);
      // Avoid 'special' prefixes
      if (!s.plaintext().startsWith("0x")) {
        return s;
      }
    }
  }
}
TOP

Related Classes of org.platformlayer.ops.crypto.Passwords

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.