/* IDCheckInputStream.java * * This file is part of Gzz. * * Gzz is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Gzz 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 Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with Gzz; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * */ /* * Written by Benja Fallenstein */ package gzz.mediaserver; import java.security.*; import java.io.*; import gzz.util.HexUtil; /** An InputStream that checks whether a given Mediaserver.Id matches. * When being closed, this throws an exception if the hash doesn't match. */ public class IDCheckInputStream extends FilterInputStream { private MessageDigest d; private DigestInputStream dis; private byte[] hash; public IDCheckInputStream(InputStream is, byte[] hash) { this(is, null, hash); } public IDCheckInputStream(InputStream is, byte[] idstart, byte[] hash) { super(new DigestInputStream(is, null)); this.dis = (DigestInputStream)this.in; try { d = MessageDigest.getInstance("SHA"); } catch(Exception e) { e.printStackTrace(); System.out.println(e); throw new Error("Can't create IDCheckInputStream: "+e); } if(idstart != null) { int l = idstart.length; d.update((byte)((l >>> 24) & 0xff)); d.update((byte)((l >>> 16) & 0xff)); d.update((byte)((l >>> 8) & 0xff)); d.update((byte)(l & 0xff)); d.update(idstart); } dis.setMessageDigest(d); this.hash = hash; } public void close() throws IOException { super.close(); byte[] dig = d.digest(); if(dig.length != hash.length) throw new Error("Hash did not match (different lengths)"); for(int i=0; i