Here you can find the source of sha256(String base)
Parameter | Description |
---|---|
base | La stringa da crittografare. |
static String sha256(String base)
//package com.java2s; /****************************************************************************** * Copyright (c) 2015 Nicola Mometto//from w ww .j a v a 2s . co m * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Nicola Mometto * Antonio Cavestro * Sebastiano Valle * Gabriele Pozzan ******************************************************************************/ import java.security.MessageDigest; public class Main { /** * Calcola l'hash crittografico di una stringa di input tramite SHA-256. * * @param base La stringa da crittografare. * @return Restituisce l'hash crittografico della stringa di input. */ static String sha256(String base) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(base.getBytes("UTF-8")); StringBuilder hexString = new StringBuilder(); for (byte b : hash) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (Exception ex) { throw new RuntimeException(ex); } } }