Compute the SHA-1 hash of the bytes in the given buffer : Buffer « File « Android






Compute the SHA-1 hash of the bytes in the given buffer

 
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
 * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol
 * This project contains two packages:
 * 1. jBittorrentAPI is the "client" part, i.e. it implements all classes needed to publish
 *    files, share them and download them.
 *    This package also contains example classes on how a developer could create new applications.
 * 2. trackerBT is the "tracker" part, i.e. it implements a all classes needed to run
 *    a Bittorrent tracker that coordinates peers exchanges. *
 *
 * Copyright (C) 2007 Baptiste Dubuis, Artificial Intelligence Laboratory, EPFL
 *
 * This file is part of jbittorrentapi-v1.0.zip
 *
 * Java Bittorrent API is free software and a free user study set-up;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Java Bittorrent API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Java Bittorrent API; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @version 1.0
 * @author Baptiste Dubuis
 * To contact the author:
 * email: baptiste.dubuis@gmail.com
 *
 * More information about Java Bittorrent API:
 *    http://sourceforge.net/projects/bitext/
 */

//package atorrentapi;


class Main {
  /**
   * Compute the SHA-1 hash of the bytes in the given buffer
   * 
   * @param hashThis
   *            ByteBuffer
   * @return byte[]
   */
  public static byte[] hash(ByteBuffer hashThis) {
    try {
      // byte[] hash = new byte[20];
      MessageDigest md = MessageDigest.getInstance("SHA-1");
      md.update(hashThis);
      return md.digest();
    } catch (NoSuchAlgorithmException nsae) {
    }
    return null;
  }



}

   
  








Related examples in the same category

1.Fast Float Buffer
2.Demonstrate the Frame Buffer Object OpenGL ES extension.
3.Loads a file to a ByteBuffer.
4.Make a direct NIO FloatBuffer from an array of floats
5.Make a direct NIO ByteBuffer from an array of bytes
6.Make Float Buffer From Array
7.Creates a floatbuffer of the given size.
8.Make Float Buffer with ByteBuffer.allocateDirect
9.allocate Float Buffer
10.to Short Buffer
11.to Float Buffer Position Zero
12.allocate Short Buffer
13.allocate Int Buffer
14.Read InputStream with BufferedReader
15.To convert the InputStream to String we use the BufferedReader.readLine() method.
16.make Float Buffer
17.get Buffer From Array
18.Your own float buffer
19.Direct Buffer
20.create Buffer
21.Write String to File with BufferWriter
22.Byte Buffer Stack
23.An utility class for java.nio.Buffers