View Javadoc

1   /*
2    * $Id: AbstractNamedEncryptionStrategy.java 22409 2011-07-14 05:14:27Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.security;
12  
13  import org.mule.api.EncryptionStrategy;
14  import org.mule.api.security.CryptoFailureException;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.IOException;
18  import java.io.InputStream;
19  
20  import org.apache.commons.io.IOUtils;
21  
22  public abstract class AbstractNamedEncryptionStrategy implements EncryptionStrategy
23  {
24  
25      private String name;
26  
27      public String getName()
28      {
29          return name;
30      }
31  
32      public void setName(String name)
33      {
34          this.name = name;
35      }
36  
37      public byte[] encrypt(byte[] data, Object info) throws CryptoFailureException {
38          InputStream io = this.encrypt(new ByteArrayInputStream(data), info);
39          try
40          {
41              return IOUtils.toByteArray(io);
42          }
43          catch (IOException e)
44          {
45              throw new CryptoFailureException(this, e);
46          }
47      }
48  
49      public byte[] decrypt(byte[] data, Object info) throws CryptoFailureException {
50          InputStream io = this.decrypt(new ByteArrayInputStream(data), info);
51          try
52          {
53              return IOUtils.toByteArray(io);
54          }
55          catch (IOException e)
56          {
57              throw new CryptoFailureException(this, e);
58          }
59      }
60  }