File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -818,8 +818,53 @@ public long checksum(){
818818 }
819819 }
820820
821+
822+ //**************************************************************************
823+ //** getSHA1
824+ //**************************************************************************
825+ /** Returns a string representing the SHA-1 hash for the file.
826+ */
827+ public String getSHA1 () {
828+ return getHash ("SHA-1" );
829+ }
830+
831+
832+ //**************************************************************************
833+ //** getMD5
834+ //**************************************************************************
835+ /** Returns a string representing the MD5 hash for the file.
836+ */
837+ public String getMD5 () {
838+ return getHash ("MD5" );
839+ }
840+
841+
842+ //**************************************************************************
843+ //** getHash
844+ //**************************************************************************
845+ private String getHash (String algorithm ){
846+ if (!exists ()) return null ;
847+ try {
848+ java .security .MessageDigest md = java .security .MessageDigest .getInstance (algorithm );
849+ InputStream input = getInputStream ();
850+ byte [] buf = new byte [bufferSize ];
851+ int i = 0 ;
852+ while ((i =input .read (buf ))!=-1 ) {
853+ md .update (buf , 0 , i );
854+ }
855+ input .close ();
856+ return new javax .xml .bind .annotation .adapters .HexBinaryAdapter ().marshal (md .digest ()).toLowerCase ();
857+ }
858+ catch (Exception e ){
859+ e .printStackTrace ();
860+ return null ;
861+ }
862+ }
821863
822864
865+ //**************************************************************************
866+ //** write
867+ //**************************************************************************
823868 public void write (ByteArrayOutputStream bas ){
824869 write (bas .toByteArray ());
825870 }
You can’t perform that action at this time.
0 commit comments