Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    196

    [JAVA] creare file di output

    Sono ancora io,ho il mio solito problema sono riuscito a creare un file che generi un impronta hash esadecimale ora vorrei che l'impronta venisse memorizzata in un file di testo come posso fare? ecco il mio code

    <code >
    package miaclasse ;
    import java.io.*;
    import java.security.*;

    public class MessageDigestEncoder {
    public static String valore;


    static private byte [] loadByteData (String filename) {
    FileInputStream fis = null;
    try {
    fis = new FileInputStream (filename);
    BufferedInputStream bis = new BufferedInputStream (fis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int ch;
    while ((ch = bis.read()) != -1) {
    baos.write (ch);
    }
    return baos.toByteArray();
    } catch (IOException e) {
    if (fis != null) {
    try {
    fis.close();
    } catch (IOException ee) {
    // ignore
    }
    }
    return null;
    }
    }
    static private String hexDigit(byte x) {
    StringBuffer sb = new StringBuffer();
    char c;
    // First nibble
    c = (char) ((x >> 4) & 0xf);
    if (c > 9) {
    c = (char) ((c - 10) + 'a');
    } else {
    c = (char) (c + '0');
    }
    sb.append (c);
    // Second nibble
    c = (char) (x & 0xf);
    if (c > 9) {
    c = (char)((c - 10) + 'a');
    } else {
    c = (char)(c + '0');
    }
    sb.append (c);
    return sb.toString();
    }
    static private String computeDigest (MessageDigest algorithm, String filename) {
    byte[] content = loadByteData (filename);
    if (content != null) {
    algorithm.reset();
    algorithm.update (content);
    byte digest[] = algorithm.digest();
    StringBuffer hexString = new StringBuffer();
    int digestLength = digest.length;
    for (int i=0;i<digestLength;i++) {
    hexString.append (hexDigit(digest[i]));
    hexString.append (" ");
    }
    return hexString.toString();
    } else {
    return "";
    }
    }
    public static void privkey()
    {
    try {
    MessageDigest algorithm = MessageDigest.getInstance("MD5");
    String filename = "c:\\t.txt";
    String digest = computeDigest (algorithm, filename);
    valore = "nome file :__ " + filename + "________impronta hash :" + digest;

    }
    catch (Exception e) {
    System.err.println (e.toString());
    }
    }

    public String verificaValore()
    {
    return valore;
    }

    }

    </code>

    grazie in anticipo

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2004
    Messaggi
    196
    dopo tanto ci sono riuscito,grazie a tutti

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.