@@ -10,15 +10,31 @@ public class TokenGenerateInput {
1010 * @return a TokenGenerateInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
1111 */
1212 public static TokenGenerateInput fromEmail (String email ) {
13- return new TokenGenerateInput (IdentityType .Email , email , true );
13+ return new TokenGenerateInput (IdentityType .Email , email , true , false );
1414 }
1515
1616 /**
1717 * @param phone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> phone number
1818 * @return a TokenGenerateInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
1919 */
2020 public static TokenGenerateInput fromPhone (String phone ) {
21- return new TokenGenerateInput (IdentityType .Phone , phone , true );
21+ return new TokenGenerateInput (IdentityType .Phone , phone , true , false );
22+ }
23+
24+ /**
25+ * @param hashedEmail a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#email-address-hash-encoding">hashed</a> email address
26+ * @return a TokenGenerateInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
27+ */
28+ public static TokenGenerateInput fromHashedEmail (String hashedEmail ) {
29+ return new TokenGenerateInput (IdentityType .Email , hashedEmail , false , true );
30+ }
31+
32+ /**
33+ * @param hashedPhone a <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-normalization">normalized</a> and <a href="https://unifiedid.com/docs/getting-started/gs-normalization-encoding#phone-number-hash-encoding">hashed</a> phone number
34+ * @return a TokenGenerateInput instance, to be used in {@link PublisherUid2Helper#createEnvelopeForTokenGenerateRequest}
35+ */
36+ public static TokenGenerateInput fromHashedPhone (String hashedPhone ) {
37+ return new TokenGenerateInput (IdentityType .Phone , hashedPhone , false , true );
2238 }
2339
2440 /**
@@ -36,32 +52,22 @@ TokenGenerateInput doNotHash() {
3652 }
3753
3854 String getAsJsonString () {
39- if (needHash ) {
55+ if (alreadyHashed ) {
56+ return createAlreadyHashedJsonRequestForGenerateToken (identityType , emailOrPhone , transparencyAndConsentString );
57+ } else if (needHash ) {
4058 return createHashedJsonRequestForGenerateToken (identityType , emailOrPhone , transparencyAndConsentString );
4159 } else {
4260 return createJsonRequestForGenerateToken (identityType , emailOrPhone , transparencyAndConsentString );
4361 }
4462 }
4563
46- private TokenGenerateInput (IdentityType identityType , String emailOrPhone , boolean needHash ) {
64+ private TokenGenerateInput (IdentityType identityType , String emailOrPhone , boolean needHash , boolean alreadyHashed ) {
4765 this .identityType = identityType ;
4866 this .emailOrPhone = emailOrPhone ;
4967 this .needHash = needHash ;
68+ this .alreadyHashed = alreadyHashed ;
5069 }
5170
52- private static String getBase64EncodedHash (String input ) {
53- return InputUtil .byteArrayToBase64 (getSha256Bytes (input ));
54- }
55-
56- private static byte [] getSha256Bytes (String input ) {
57- try {
58- MessageDigest md = MessageDigest .getInstance ("SHA-256" );
59- md .update (input .getBytes ());
60- return md .digest ();
61- } catch (Exception e ) {
62- throw new Uid2Exception ("Trouble Generating SHA256" , e );
63- }
64- }
6571
6672
6773 private static String createJsonRequestForGenerateToken (IdentityType identityType , String value , String tcString ) {
@@ -86,20 +92,31 @@ static String createHashedJsonRequestForGenerateToken(IdentityType identityType,
8692 if (normalizedEmail == null ) {
8793 throw new IllegalArgumentException ("invalid email address" );
8894 }
89- String hashedNormalizedEmail = getBase64EncodedHash (normalizedEmail );
95+ String hashedNormalizedEmail = InputUtil . getBase64EncodedHash (normalizedEmail );
9096 return createJsonRequestForGenerateToken ("email_hash" , hashedNormalizedEmail , tcString );
9197 } else { //phone
9298 if (!InputUtil .isPhoneNumberNormalized (unhashedValue )) {
9399 throw new IllegalArgumentException ("phone number is not normalized" );
94100 }
95101
96- String hashedNormalizedPhone = getBase64EncodedHash (unhashedValue );
102+ String hashedNormalizedPhone = InputUtil . getBase64EncodedHash (unhashedValue );
97103 return createJsonRequestForGenerateToken ("phone_hash" , hashedNormalizedPhone , tcString );
98104 }
99105 }
100106
107+ static String createAlreadyHashedJsonRequestForGenerateToken (IdentityType identityType , String hashedValue , String tcString ) {
108+ if (identityType == IdentityType .Email ) {
109+ return createJsonRequestForGenerateToken ("email_hash" , hashedValue , tcString );
110+ } else { //phone
111+ return createJsonRequestForGenerateToken ("phone_hash" , hashedValue , tcString );
112+ }
113+ }
114+
101115 private final IdentityType identityType ;
102116 private final String emailOrPhone ;
103117 private boolean needHash ;
118+ private boolean alreadyHashed ;
104119 private String transparencyAndConsentString ;
120+
121+
105122}
0 commit comments