-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/org/b3log/symphony/repository/UploadRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Rhythm - A modern community (forum/BBS/SNS/blog) platform written in Java. | ||
* Modified version from Symphony, Thanks Symphony :) | ||
* Copyright (C) 2012-present, b3log.org | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package org.b3log.symphony.repository; | ||
|
||
import org.b3log.latke.repository.AbstractRepository; | ||
import org.b3log.latke.repository.RepositoryException; | ||
import org.b3log.latke.repository.annotation.Repository; | ||
import org.json.JSONObject; | ||
|
||
@Repository | ||
public class UploadRepository extends AbstractRepository { | ||
/** | ||
* Public constructor. | ||
*/ | ||
public UploadRepository() { | ||
super("upload"); | ||
} | ||
|
||
public void add(String type, String userName, String ip, String time, String path, String md5, boolean isPublic) throws RepositoryException { | ||
JSONObject jsonObject = new JSONObject(); | ||
jsonObject.put("type", type); | ||
jsonObject.put("userName", userName); | ||
jsonObject.put("ip", ip); | ||
jsonObject.put("time", time); | ||
jsonObject.put("path", path); | ||
jsonObject.put("md5", md5); | ||
jsonObject.put("public", isPublic); | ||
add(jsonObject); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.b3log.symphony.util; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
|
||
public class MD5Calculator { | ||
|
||
public static String bytesToHex(byte[] hash) { | ||
StringBuilder hexString = new StringBuilder(2 * hash.length); | ||
for (int i = 0; i < hash.length; i++) { | ||
String hex = Integer.toHexString(0xff & hash[i]); | ||
if(hex.length() == 1) { | ||
hexString.append('0'); | ||
} | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} | ||
|
||
public static String calculateMd5(List<byte[]> fileBytes) { | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance("MD5"); | ||
|
||
// 遍历每个byte[]并更新digest | ||
for (byte[] bytes : fileBytes) { | ||
digest.update(bytes); | ||
} | ||
|
||
// 完成哈希计算 | ||
byte[] hash = digest.digest(); | ||
|
||
// 将字节数组转换为十六进制字符串 | ||
return bytesToHex(hash); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException("MD5 algorithm not found", e); | ||
} | ||
} | ||
|
||
public static String calculateMd5(byte[] input) { | ||
try { | ||
MessageDigest digest = MessageDigest.getInstance("MD5"); | ||
byte[] hash = digest.digest(input); // 计算MD5散列 | ||
|
||
// 将字节数组转换为十六进制字符串 | ||
StringBuilder hexString = new StringBuilder(); | ||
for (byte b : hash) { | ||
String hex = Integer.toHexString(0xFF & b); | ||
if (hex.length() == 1) { | ||
hexString.append('0'); | ||
} | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException("MD5 algorithm not found", e); | ||
} | ||
} | ||
} |