Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.pfabulist.lindwurm.stellvertreter;
  
  
  import java.io.*;
 
 import static java.nio.file.StandardOpenOption.*;
 import static de.pfabulist.kleinod.errors.Unchecked.runtime;
 import static de.pfabulist.kleinod.text.Strings.newString;
 import static de.pfabulist.lindwurm.stellvertreter.StellvertreterConstants.BLOCK_SIZE;

** BEGIN LICENSE BLOCK ***** BSD License (2 clause) Copyright (c) 2006 - 2014, Stephan Pfab All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Stephan Pfab BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. **** END LICENSE BLOCK ****
 
 public class CopyEncrypted {
 
     private static final int BUFFER_SIZE = 8096;
 
     private final DeEnc denc;
     private Base16 base16 = new Base16();
 
     public CopyEncrypted(DeEnc denc) {
         this. = denc;
     }
 
     public void copyEncryptPath srcPath tgt ) throws IOException {
 
         Files.createDirectoriestgt.getParent());
 
         Cipher ecipher = .getEncryptingCipher();
 
         byte[] iv = ecipher.getIV();
         byte[] content = new byte[ 8 + ];
         System.arraycopy.getSalt(), 0, content, 0, 8 );
         System.arraycopyiv, 0, content, 8,   );
 
         Files.writetgtcontent );
 
 
         try (InputStream in   = Files.newInputStream(src);
              OutputStream out = Files.newOutputStreamtgt )) {
 
             tryCipherOutputStream cout = new CipherOutputStreamoutecipher )) {
                 copy(incout);
             }
         }
     }
 
     public String hash(String str) {
         try {
             MessageDigest md = MessageDigest.getInstance("sha1");
             md.updatestr.getBytes("UTF-8") );
             return .encrypt(md.digest());
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
         return "";
     }
 
     public String copyEncryptString src ) throws IOException {
 
         Cipher ecipher = .getEncryptingCipher();
 
         byte[] iv = ecipher.getIV();
         //byte[] content = new byte[ BLOCK_SIZE];
         //System.arraycopy( denc.getSalt(), 0, content, 0, 8 );
        //System.arraycopy( iv, 0, content, 0, BLOCK_SIZE  );
        try (InputStream in   = new ByteArrayInputStreamsrc.getBytes("UTF-8"));
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            out.writehash(src).getBytes("UTF-8"));
            out.writeiv );
            tryCipherOutputStream cout = new CipherOutputStreamoutecipher )) {
                copy(incout);
            }
            return Strings.asHex(out.toByteArray());
        }
    }
    public String copyDecryptString src ) {
        try (InputStream in   = new ByteArrayInputStream.decrypt(src));
             ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] hash = new byte[40];
            in.read(hash);
            byte[] iv = new byte[];
            in.read(iv);
            tryCipherInputStream cin = new CipherInputStreamin.getDecryptingCipheriv ) )) {
                copy(cinout);
            }
            return newStringout.toByteArray());
        } catch (IOException e ) {
            throw runtime(e);
        }
    }
    public void copyDecryptPath srcPath tgt ) {
        try (InputStream in   = Files.newInputStream(src);
             OutputStream out = Files.newOutputStreamtgt )) {
            byte[] both = new byte[ 8 + ];
            in.read(both);
            byte[] iv = new byte[];
            System.arraycopyboth, 8, iv, 0,   );
            tryCipherInputStream cin = new CipherInputStreamin.getDecryptingCipheriv ) )) {
                copy(cinout);
            }
        } catch (IOException e ) {
            throw runtime(e);
        }
    }
    private static long copy(InputStream sourceOutputStream sink)
            throws IOException
    {
        long nread = 0L;
        byte[] buf = new byte[];
        int n;
        while ((n = source.read(buf)) > 0) {
            sink.write(buf, 0, n);
            nread += n;
        }
        return nread;
    }
New to GrepCode? Check out our FAQ X