<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 * @copyright Copyright (c) 2012 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flKeygen Class
 *
 * Class to generate keys of an arbitrary size in the form AAAA-AAAA-AAAA.
 *
 * @package fusionLib
 */
class flKeygen {

	/**
	 * Create a key string.
	 *
	 * @param int $length The length of the key in characters excluding separator characters.
	 * @param int $chunkLen The number of characters in each chunk.
	 * @param string $separator The character to separate chunks.
	 * @return string The generated key.
	 */
	static function createKey($length = 12, $chunkLen = 3, $separator = '-') {
		$key = '';

		// Get the key of the required length
		do {
			$key .= base_convert(sha1(uniqid(time())), 16, 36);
		} while(strlen($key) < $length);
		$key = strtoupper(substr($key, 0, $length));

		// Break into chunks
		return substr(chunk_split($key, $chunkLen, $separator), 0, -strlen($separator));
	}
}
