Java tutorial
// Copyright 2015 Moritz Petersen // // Licensed 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 org.cloudme.armada.util; import java.security.SecureRandom; import com.google.appengine.repackaged.org.apache.commons.codec.binary.Hex; import com.google.common.hash.Hashing; /** * Some utility methods to work with tokens. * * @author Moritz Petersen */ public class TokenUtils { private static SecureRandom secureRandom = new SecureRandom(); private TokenUtils() { // don't instantiate } /** * Generates a random code of the given number of bytes. * * @param size * Size in bytes. * @return A random code. */ public static String randomCode(int size) { byte[] bytes = new byte[size]; secureRandom.nextBytes(bytes); return Hex.encodeHexString(bytes); } /** * Creates a hash of the given value. The hashing algorithm is currently SHA-256. * * @param value * The original value. * @return The hashed value. */ public static String hash(String value) { return Hashing.sha256().hashBytes(value.getBytes()).toString(); } }