All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jdk7Main.common.Base62.kt Maven / Gradle / Ivy

package io.fluidsonic.stdlib

import java.security.*


object Base62 {

	private val characters = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
	private val random = SecureRandom()


	fun random(length: Int): String {
		check(length >= 0) { "'length' must not be negative" }

		if (length == 0)
			return ""

		return buildString {
			repeat(length) {
				append(randomCharacter())
			}
		}
	}


	fun randomCharacter() =
		characters[random.nextInt(characters.size)]
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy