View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.api.ldap.model.ldif.anonymizer;
22  
23  
24  import java.util.Random;
25  
26  import org.apache.directory.api.ldap.model.entry.Attribute;
27  import org.apache.directory.api.ldap.model.entry.BinaryValue;
28  import org.apache.directory.api.ldap.model.entry.DefaultAttribute;
29  import org.apache.directory.api.ldap.model.entry.Value;
30  import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
31  
32  
33  /**
34   * A default anonymizer for attributes that are not HR
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class BinaryAnonymizer implements Anonymizer
39  {
40      /** Create a random generator */
41      Random random = new Random( System.currentTimeMillis() );
42  
43  
44      /**
45       * Anonymize an attribute using pure random values (either chars of bytes, depending on the Attribute type)
46       */
47      @Override
48      public Attribute anonymize( Attribute attribute )
49      {
50          Attribute result = new DefaultAttribute( attribute.getAttributeType() );
51          random.setSeed( System.nanoTime() );
52  
53          for ( Value<?> value : attribute )
54          {
55              if ( value instanceof BinaryValue )
56              {
57                  byte[] bytesValue = value.getBytes();
58  
59                  int length = bytesValue.length;
60  
61                  // Same size
62                  byte[] newValue = new byte[length];
63  
64                  for ( int i = 0; i < length; i++ )
65                  {
66                      newValue[i] = ( byte ) ( random.nextInt( 'Z' - 'A' ) + 'A' );
67                  }
68  
69                  try
70                  {
71                      result.add( newValue );
72                  }
73                  catch ( LdapInvalidAttributeValueException e )
74                  {
75                      // TODO : handle that
76                  }
77              }
78              else
79              {
80                  byte[] byteValue = value.getBytes();
81  
82                  // Same size
83                  byte[] newValue = new byte[byteValue.length];
84  
85                  for ( int i = 0; i < byteValue.length; i++ )
86                  {
87                      newValue[i] = ( byte ) random.nextInt();
88                  }
89  
90                  try
91                  {
92                      result.add( newValue );
93                  }
94                  catch ( LdapInvalidAttributeValueException e )
95                  {
96                      // TODO : handle that
97                  }
98              }
99          }
100 
101         return result;
102     }
103 }