1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.DefaultAttribute;
28 import org.apache.directory.api.ldap.model.entry.StringValue;
29 import org.apache.directory.api.ldap.model.entry.Value;
30 import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
31
32
33
34
35
36
37
38 public class StringAnonymizer implements Anonymizer
39 {
40
41 Random random = new Random( System.currentTimeMillis() );
42
43
44
45
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 StringValue )
56 {
57 String strValue = value.getString();
58
59 int length = strValue.length();
60
61
62 char[] newValue = new char[length];
63
64 for ( int i = 0; i < length; i++ )
65 {
66 newValue[i] = ( char ) ( random.nextInt( 'Z' - 'A' ) + 'A' );
67 }
68
69 try
70 {
71 result.add( new String( newValue ) );
72 }
73 catch ( LdapInvalidAttributeValueException e )
74 {
75
76 }
77 }
78 else
79 {
80 byte[] byteValue = value.getBytes();
81
82
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
97 }
98 }
99 }
100
101 return result;
102 }
103 }