11package com .qiniu .android .storage .persistent ;
22
33import com .qiniu .android .storage .Recorder ;
4- import com .qiniu .android .utils .UrlSafeBase64 ;
54
65import java .io .File ;
76import java .io .FileInputStream ;
87import java .io .FileOutputStream ;
98import java .io .IOException ;
9+ import java .security .MessageDigest ;
1010import java .util .Date ;
1111
1212/**
@@ -39,7 +39,7 @@ public FileRecorder(String directory) throws IOException {
3939 */
4040 @ Override
4141 public void set (String key , byte [] data ) {
42- File f = new File (directory , UrlSafeBase64 . encodeToString (key ));
42+ File f = new File (directory , sha256 (key ));
4343 FileOutputStream fo = null ;
4444 try {
4545 fo = new FileOutputStream (f );
@@ -63,7 +63,7 @@ public void set(String key, byte[] data) {
6363 */
6464 @ Override
6565 public byte [] get (String key ) {
66- File f = new File (directory , UrlSafeBase64 . encodeToString (key ));
66+ File f = new File (directory , sha256 (key ));
6767 FileInputStream fi = null ;
6868 byte [] data = null ;
6969 int read = 0 ;
@@ -102,7 +102,26 @@ private boolean outOfDate(File f) {
102102 */
103103 @ Override
104104 public void del (String key ) {
105- File f = new File (directory , UrlSafeBase64 . encodeToString (key ));
105+ File f = new File (directory , sha256 (key ));
106106 f .delete ();
107107 }
108+
109+ private static String sha256 (String base ) {
110+ try {
111+ MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
112+ byte [] hash = digest .digest (base .getBytes ());
113+ StringBuffer hexString = new StringBuffer ();
114+
115+ for (int i = 0 ; i < hash .length ; i ++) {
116+ String hex = Integer .toHexString (0xff & hash [i ]);
117+ if (hex .length () == 1 ) hexString .append ('0' );
118+ hexString .append (hex );
119+ }
120+
121+ return hexString .toString ();
122+ } catch (Exception ex ) {
123+ ex .printStackTrace ();
124+ }
125+ return null ;
126+ }
108127}
0 commit comments