rabbah closed pull request #3396: Self-generate random strings without going through a UUID. URL: https://github.com/apache/incubator-openwhisk/pull/3396
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/common/scala/src/main/scala/whisk/common/RandomString.scala b/common/scala/src/main/scala/whisk/common/RandomString.scala new file mode 100644 index 0000000000..b30f432458 --- /dev/null +++ b/common/scala/src/main/scala/whisk/common/RandomString.scala @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package whisk.common + +import java.security.SecureRandom + +import org.apache.commons.codec.binary.Hex + +object RandomString { + private val generator = new ThreadLocal[SecureRandom] { + override def initialValue() = new SecureRandom() + } + + val lowercase: IndexedSeq[Char] = 'a' to 'z' + val uppercase: IndexedSeq[Char] = 'A' to 'Z' + val digits: IndexedSeq[Char] = '0' to '9' + val alphanumeric: IndexedSeq[Char] = lowercase ++ uppercase ++ digits + + /** + * Generates a random string of hexadecimal characters and the given length. + * + * Vastly optimized for raw speed over the other generate methods. For that optimization, the length of the string + * needs to be divisible by 2 (4 bits make one hex character and a byte consists of 8 bits). If the number is not + * divisible by two, the outcome string will be one character shorter than expected. + * + * @param length length of the string to generate + * @return a random string + */ + def generateHexadecimal(length: Int): String = { + val bytes = Array.ofDim[Byte](length / 2) + generator.get().nextBytes(bytes) + Hex.encodeHexString(bytes) + } + + /** + * Generates a random string of the given character range and the given length. + * + * @param characters the characters to build the target string of + * @param length length of the target string + * @return a random string + */ + def generate(characters: IndexedSeq[Char])(length: Int): String = { + val random = generator.get() + Stream.continually(characters(random.nextInt(characters.length))).take(length).mkString + } +} diff --git a/common/scala/src/main/scala/whisk/core/entity/ActivationId.scala b/common/scala/src/main/scala/whisk/core/entity/ActivationId.scala index 85c59beb7b..e5ad3a79be 100644 --- a/common/scala/src/main/scala/whisk/core/entity/ActivationId.scala +++ b/common/scala/src/main/scala/whisk/core/entity/ActivationId.scala @@ -19,8 +19,8 @@ package whisk.core.entity import spray.json.DefaultJsonProtocol.StringJsonFormat import spray.json._ +import whisk.common.RandomString import whisk.http.Messages - import whisk.core.entity.size._ import scala.util.{Failure, Success, Try} @@ -74,7 +74,7 @@ protected[core] object ActivationId { * * @return new ActivationId */ - protected[core] def generate(): ActivationId = new ActivationId(UUIDs.randomUUID().toString.filterNot(_ == '-')) + protected[core] def generate(): ActivationId = new ActivationId(RandomString.generateHexadecimal(32)) protected[core] implicit val serdes: RootJsonFormat[ActivationId] = new RootJsonFormat[ActivationId] { def write(d: ActivationId) = JsString(d.toString) diff --git a/common/scala/src/main/scala/whisk/core/entity/Secret.scala b/common/scala/src/main/scala/whisk/core/entity/Secret.scala index aac4e7f7e8..c320b76dfb 100644 --- a/common/scala/src/main/scala/whisk/core/entity/Secret.scala +++ b/common/scala/src/main/scala/whisk/core/entity/Secret.scala @@ -19,6 +19,7 @@ package whisk.core.entity import spray.json._ import spray.json.DefaultJsonProtocol._ +import whisk.common.RandomString /** * Secret, a cryptographic string such as a key used for authentication. @@ -57,19 +58,11 @@ protected[core] object Secret { new Secret(str) } - /** - * Creates a new random secret. - * - * @return Secret - */ - protected[core] def apply(): Secret = { - Secret(rand.alphanumeric.take(MIN_LENGTH).mkString) - } + /** Creates a new random secret. */ + protected[core] def apply(): Secret = Secret(RandomString.generate(RandomString.alphanumeric)(MIN_LENGTH)) implicit val serdes: RootJsonFormat[Secret] = new RootJsonFormat[Secret] { def write(s: Secret): JsValue = s.toJson def read(value: JsValue): Secret = Secret(value.convertTo[String]) } - - private val rand = new scala.util.Random(new java.security.SecureRandom()) } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
