1package com.bumptech.glide.load.engine.cache; 2 3import com.bumptech.glide.load.Key; 4import com.bumptech.glide.util.LruCache; 5import com.bumptech.glide.util.Util; 6 7import java.io.UnsupportedEncodingException; 8import java.security.MessageDigest; 9import java.security.NoSuchAlgorithmException; 10 11/** 12 * A class that generates and caches safe and unique string file names from {@link com.bumptech.glide.load.Key}s. 13 */ 14class SafeKeyGenerator { 15 private final LruCache<Key, String> loadIdToSafeHash = new LruCache<Key, String>(1000); 16 17 public String getSafeKey(Key key) { 18 String safeKey; 19 synchronized (loadIdToSafeHash) { 20 safeKey = loadIdToSafeHash.get(key); 21 } 22 if (safeKey == null) { 23 try { 24 MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); 25 key.updateDiskCacheKey(messageDigest); 26 safeKey = Util.sha256BytesToHex(messageDigest.digest()); 27 } catch (UnsupportedEncodingException e) { 28 e.printStackTrace(); 29 } catch (NoSuchAlgorithmException e) { 30 e.printStackTrace(); 31 } 32 synchronized (loadIdToSafeHash) { 33 loadIdToSafeHash.put(key, safeKey); 34 } 35 } 36 return safeKey; 37 } 38} 39