BVB Source Codes

ForkHub Show GravatarUtils.java Source code

Return Download ForkHub: download GravatarUtils.java Source code - Download ForkHub Source code - Type:.java
  1. /*
  2.  * Copyright 2012 GitHub Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *  http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.github.mobile.util;
  17.  
  18. import static java.util.Locale.US;
  19. import android.text.TextUtils;
  20.  
  21. import java.io.UnsupportedEncodingException;
  22. import java.math.BigInteger;
  23. import java.security.MessageDigest;
  24. import java.security.NoSuchAlgorithmException;
  25. import java.util.Arrays;
  26.  
  27. /**
  28.  * Helper to get a gravatar hash for an email
  29.  */
  30. public class GravatarUtils {
  31.  
  32.     /**
  33.      * Length of generated hash
  34.      */
  35.     private static final int HASH_LENGTH = 32;
  36.  
  37.     /**
  38.      * Charset used for hashing
  39.      */
  40.     private static final String CHARSET = "CP1252";
  41.  
  42.     /**
  43.      * Algorithm used for hashing
  44.      */
  45.     private static final MessageDigest MD5;
  46.  
  47.     static {
  48.         MessageDigest digest;
  49.         try {
  50.             digest = MessageDigest.getInstance("MD5");
  51.         } catch (NoSuchAlgorithmException e) {
  52.             digest = null;
  53.         }
  54.         MD5 = digest;
  55.     }
  56.  
  57.     private static String digest(final String value) {
  58.         if (MD5 == null)
  59.             return null;
  60.  
  61.         byte[] bytes;
  62.         try {
  63.             bytes = value.getBytes(CHARSET);
  64.         } catch (UnsupportedEncodingException e) {
  65.             return null;
  66.         }
  67.  
  68.         synchronized (MD5) {
  69.             MD5.reset();
  70.             bytes = MD5.digest(bytes);
  71.         }
  72.  
  73.         String hashed = new BigInteger(1, bytes).toString(16);
  74.         int padding = HASH_LENGTH - hashed.length();
  75.         if (padding == 0)
  76.             return hashed;
  77.  
  78.         char[] zeros = new char[padding];
  79.         Arrays.fill(zeros, '0');
  80.         return new StringBuilder(HASH_LENGTH).append(zeros).append(hashed)
  81.                 .toString();
  82.     }
  83.  
  84.     /**
  85.      * Get avatar hash for specified e-mail address
  86.      *
  87.      * @param email
  88.      * @return hash
  89.      */
  90.     public static String getHash(String email) {
  91.         if (TextUtils.isEmpty(email))
  92.             return null;
  93.         email = email.trim().toLowerCase(US);
  94.         return email.length() > 0 ? digest(email) : null;
  95.     }
  96. }
downloadGravatarUtils.java Source code - Download ForkHub Source code
Related Source Codes/Software:
css-element-queries - CSS-Element-Queries Polyfill. proof-of-concept for... 2017-05-09
shortid - Short id generator. Url-friendly. Non-predictable.... 2017-05-09
philm - Movie collection and information app for Android. 2017-05-09
DIGITS - Deep Learning GPU Training System ... 2017-05-09
skatejs - SkateJS is a web component library designed to giv... 2017-05-09
i18n-js - It's a small library to provide the I18n translati... 2017-05-09
api-standards - 2017-05-09
MiaowShow - IOS video broadcast program 2017-05-09
csscomb.js - CSS coding style formatter (Maintenance) ... 2017-05-09
LocationManager - Easily get the device's current location on iOS. 2017-05-09
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top