我们在工作中可能会遇到大文件的上传,如果一次传输,很有可能会遇到 网络、超时各种各样的问题。这里将分片上传和断点续传两种方式介绍一下方案。
一、分片上传
分片上传的原理:前端首先请求接口获取这次上传的uuid,上传的时间等信息返回给前端页面。前端将所要上传文件进行分片,在上传的时候包含 服务器返回给前端的 该次上传的uuid,该篇文件的md5,上传的第几片的的标识,和文件名称等标识。等到上传的片数等于总片数的时候对所有分片进行合并,删除临时分片。
这里由springboot+freemarker 来演示这个原理
(服务端用到工具类和前端用到的 js由于断点续传 同样也用到,在介绍完 断点续传 之后统一贴出来)
1、首先添加配置文件 application.properties(由于断点续传 需要依赖mysql故也将mysql引入)
server.port=8080
spring.freemarker.expose-spring-macro-helpers=true
# 是否优先从文件系统加载template,以支持热加载,默认为true
spring.freemarker.prefer-file-system-access=true
# 设定模板的后缀.
spring.freemarker.suffix=.ftl
# 设定模板的加载路径,多个以逗号分隔,默认:
spring.freemarker.template-loader-path=classpath:/templates/
spring.mvc.static-path-pattern=/static/**
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pingyougou
spring.datasource.username=root
spring.datasource.password=root
#配置.xml文件路径
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:conf/mapper/*Mapper.xml
#配置模型路径
mybatis.type-aliases-package=com.yin.freemakeradd.pojo
2、引入controller类
localhost:8080/upload2/index 第断点上传入口
@Controller
@RequestMapping(value = "/test")
public class IndexController {
@GetMapping(value = "/upload/index")
public String index(Model model){
return "breakpointBurst";
}
@GetMapping(value = "/upload2/index")
public String index2(Model model){
return "burst";
}
}
分片上传的具体上传逻辑
@RestController
@RequestMapping(value = "/burst/test")
public class BurstController {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
private static final String temp_dir = "C:\\Users\\Administrator\\Desktop\\upload";
private static final String final_dir = "C:\\Users\\Administrator\\Desktop\\test";
/**
* 上传文件
*
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value = "/upload")
public Map upload(
HttpServletRequest request, @RequestParam(value = "data",required = false) MultipartFile multipartFile) throws IllegalStateException, IOException, Exception {
String uuid = request.getParameter("uuid");
String fileName = request.getParameter("name");
//总大小
String size = request.getParameter("size");
//总片数
int total = Integer.valueOf(request.getParameter("total"));
//当前是第几片
int index = Integer.valueOf(request.getParameter("index"));
//整个文件的md5
String fileMd5 = request.getParameter("filemd5");
//文件第一个分片上传的日期(如:20200118)
String date = request.getParameter("date");
//分片的md5
String md5 = request.getParameter("md5");
//生成上传文件的路径信息,按天生成
String savePath = "\\upload" + File.separator + date;
String saveDirectory = temp_dir + savePath +File.separator+uuid;
//验证路径是否存在,不存在则创建目录
File path = new File(saveDirectory);
if (!path.exists()) {
path.mkdirs();
}
//文件分片位置
//File file = new File(saveDirectory, uuid + "_" + index);
multipartFile.transferTo(new File(saveDirectory, uuid + "_" + index));
if (path.isDirectory()) {
File[] fileArray = path.listFiles();
if (fileArray != null) {
if (fileArray.length == total) {
//分块全部上传完毕,合并
String suffix = NameUtil.getExtensionName(fileName);
String dir = final_dir + savePath;
File newFile = new File(dir, uuid + "." + suffix);
File copyDir = new File(dir);
if(!copyDir.mkdir()){
copyDir.mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(newFile, true);//文件追加写入
byte[] byt = new byte[10 * 1024 * 1024];
int len;
// FileInputStream temp = null;//分片文件
for (int i = 0; i < total; i++) {
int j = i + 1;
FileInputStream temp = new FileInputStream(new File(saveDirectory, uuid + "_" + j));
while ((len = temp.read(byt)) != -1) {
System.out.println("-----" + len);
outputStream.write(byt, 0, len);
}
//关闭流
temp.close();
}
outputStream.close();
//删除临时文件
FileUtil.deleteFolder( temp_dir+ savePath +File.separator+uuid);
}
}
}
HashMap map = new HashMap<>();
map.put("fileId", uuid);
return map;
}
/**
* 上传文件前获取id和日期(如果是分片上传这一步可以交给前端处理)
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "/isUpload")
public Map getUpload(HttpServletRequest request) throws Exception {
HashMap map = new HashMap<>();
String uuid = UUID.randomUUID().toString();
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
return map;
}
}
分片前端展示:
HTML5大文件分片上传示例
等待
用时
uuid==
param==
分片上传每当一次上传失败,都需要重新生成一个上传唯一uuid,全部从头开始上传。但是相对后面断点续传 同样有上传与服务器的交互少了一半,不用依赖数据库的优点。
二、断点续传
断点续传的原理:前端首先请求接口通过上传文件的MD5来获取此次是否上传,如果上传通过md5将上一次的uuid返回给前端。前端将所要上传文件进行分片,在上传的时候包含 服务器返回给前端的 该次上传的uuid,该篇文件的md5,上传的第几片的的标识,首先去检查是否此片是否上传,如果上传,直接跳到下一片的检查。如果没有上传,再上传文件。上传第一片的时候将信息记录到mysql。等上传完毕,将文件信息改为上传成功。删除临时文件夹。
1、由于首先添加配置文件 application.properties和 入口controller已经添加,这里不再继续贴出。
2、上传具体controller
@RestController
@RequestMapping(value = "/breakpointBusrt/test")
public class BreakpointBurstController {
@Autowired
private FileUploadService fileUploadService;
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
private static final String temp_dir = "C:\\Users\\Administrator\\Desktop\\upload";
private static final String final_dir = "C:\\Users\\Administrator\\Desktop\\test";
/**
* 上传文件
*
* @param request
* @return
* @throws IllegalStateException
* @throws IOException
*/
@RequestMapping(value = "/upload")
public Map upload(
HttpServletRequest request, @RequestParam(value = "data",required = false) MultipartFile multipartFile) throws IllegalStateException, IOException, Exception {
String action = request.getParameter("action");
String uuid = request.getParameter("uuid");
String fileName = request.getParameter("name");
String size = request.getParameter("size");//总大小
int total = Integer.valueOf(request.getParameter("total"));//总片数
int index = Integer.valueOf(request.getParameter("index"));//当前是第几片
String fileMd5 = request.getParameter("filemd5"); //整个文件的md5
String date = request.getParameter("date"); //文件第一个分片上传的日期(如:20170122)
String md5 = request.getParameter("md5"); //分片的md5
//生成上传文件的路径信息,按天生成
String savePath = "\\upload" + File.separator + date;
String saveDirectory = temp_dir + savePath +File.separator+uuid;
//验证路径是否存在,不存在则创建目录
File path = new File(saveDirectory);
if (!path.exists()) {
path.mkdirs();
}
//文件分片位置
File file = new File(saveDirectory, uuid + "_" + index);
//根据action不同执行不同操作. check:校验分片是否上传过; upload:直接上传分片
Map map = null;
if("check".equals(action)){
String md5Str = FileMd5Util.getFileMD5(file);
if (md5Str != null && md5Str.length() == 31) {
System.out.println("check length =" + md5.length() + " md5Str length" + md5Str.length() + " " + md5 + " " + md5Str);
md5Str = "0" + md5Str;
}
if (md5Str != null && md5Str.equals(md5)) {
//分片已上传过
map = new HashMap<>();
map.put("flag", "2");
map.put("fileId", uuid);
map.put("status", true);
return map;
} else {
//分片未上传
map = new HashMap<>();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("status", true);
return map;
}
}else if("upload".equals(action)){
//分片上传过程中出错,有残余时需删除分块后,重新上传
if (file.exists()) {
file.delete();
}
multipartFile.transferTo(new File(saveDirectory, uuid + "_" + index));
}
if (path.isDirectory()) {
File[] fileArray = path.listFiles();
if (fileArray != null) {
if (fileArray.length == total) {
//分块全部上传完毕,合并
String suffix = NameUtil.getExtensionName(fileName);
String dir = final_dir + savePath;
File newFile = new File(dir, uuid + "." + suffix);
File copyDir = new File(dir);
if(!copyDir.mkdir()){
copyDir.mkdirs();
}
//文件追加写入
FileOutputStream outputStream = new FileOutputStream(newFile, true);
byte[] byt = new byte[10 * 1024 * 1024];
int len;
//FileInputStream temp = null;//分片文件
for (int i = 0; i < total; i++) {
int j = i + 1;
FileInputStream temp = new FileInputStream(new File(saveDirectory, uuid + "_" + j));
while ((len = temp.read(byt)) != -1) {
System.out.println("-----" + len);
outputStream.write(byt, 0, len);
}
//关闭流
temp.close();
}
outputStream.close();
//修改FileRes记录为上传成功
fileUploadService.updateUploadRecord(fileMd5, FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus());
}else if(index == 1){
//文件第一个分片上传时记录到数据库
FileUploadRecord fileUploadRecord = new FileUploadRecord();
String name = NameUtil.getFileNameNoEx(fileName);
if (name.length() > 50) {
name = name.substring(0, 50);
}
String suffix = NameUtil.getExtensionName(fileName);
fileUploadRecord.setName(name);
fileUploadRecord.setTimeContent(date);
fileUploadRecord.setUuid(uuid);
fileUploadRecord.setPath(savePath + File.separator + uuid + "." + suffix);
fileUploadRecord.setSize(size);
fileUploadRecord.setMd5(fileMd5);
fileUploadRecord.setStatus(FileConstant.FileStatusEnum.UPLOAD_BEGIN.getStatus());
fileUploadService.insertUploadRecord(fileUploadRecord);
}
}
}
map = new HashMap<>();
map.put("flag", "3");
map.put("fileId", uuid);
map.put("status", true);
return map;
}
/**
* 上传文件前校验
*
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value = "/isUpload")
public Map isUpload(HttpServletRequest request) throws Exception {
String fileMd5 = request.getParameter("md5");
FileUploadRecord fileUploadRecord=fileUploadService.findByFileMd5(fileMd5);
HashMap map = new HashMap<>();
if(Objects.isNull(fileUploadRecord)){
//没有上传过文件
String uuid = UUID.randomUUID().toString();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
map.put("status", true);
return map;
}
if(FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus().equals(fileUploadRecord.getStatus())){
//文件上传成功
map.put("flag", "3");
map.put("fileId", fileUploadRecord.getUuid());
map.put("date",fileUploadRecord.getTimeContent());
map.put("status", true);
return map;
}
if(FileConstant.FileStatusEnum.UPLOAD_FINSH.getStatus().equals(fileUploadRecord.getStatus())){
map.put("flag", "1");
map.put("fileId", fileUploadRecord.getUuid());
map.put("date", fileUploadRecord.getTimeContent());
map.put("status", true);
return map;
}
//状态不对
String uuid = UUID.randomUUID().toString();
map.put("flag", "1");
map.put("fileId", uuid);
map.put("date", formatter.format(LocalDateTime.now()));
map.put("status", true);
return map;
}
}
3、上传的前端页面
HTML5大文件分片上传示例
等待
用时
uuid==
param==
这里用到mybatis,mybatis大家都有用到。这里就仅将使用到建表语句和实体类贴出
DROP TABLE IF EXISTS `upload_file_record`;
CREATE TABLE `upload_file_record` (
`uuid` varchar(50) not null,
`name` varchar(50) not null COMMENT '文件名',
`timeContent` varchar(10) not NULL COMMENT 'yyyyMMdd',
`c_path` varchar(225) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT '文件地址',
`c_size` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT '大小',
`file_md5` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL COMMENT 'fileMd5',
`status` TINYINT(2) not NULL COMMENT '文件上传状态',
`createTime` TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`uuid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
实体类:
package com.yin.freemakeradd.pojo;
import com.yin.freemakeradd.utils.NameUtil;
import java.io.File;
/**
* @author yin
* @Date 2020/1/17 22:31
* @Method
*/
@Data
public class FileUploadRecord {
private String uuid;
private String name;
private String timeContent;
private String path;
private String size;
private String md5;
private Integer status;
private String createTime;
附(这里将使用到工具类和md5.js贴出):
1、服务端使用md5生成工具
public class FileMd5Util {
public static final String KEY_MD5 = "MD5";
public static final String CHARSET_ISO88591 = "ISO-8859-1";
/**
* Get MD5 of one file:hex string,test OK!
*
* @param file
* @return
*/
public static String getFileMD5(File file) {
if (!file.exists() || !file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BigInteger bigInt = new BigInteger(1, digest.digest());
return bigInt.toString(16);
}
/***
* Get MD5 of one file!test ok!
*
* @param filepath
* @return
*/
public static String getFileMD5(String filepath) {
File file = new File(filepath);
return getFileMD5(file);
}
/**
* MD5 encrypt,test ok
*
* @param data
* @return byte[]
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
public static byte[] encryptMD5(String data) throws Exception {
return encryptMD5(data.getBytes(CHARSET_ISO88591));
}
/***
* compare two file by Md5
*
* @param file1
* @param file2
* @return
*/
public static boolean isSameMd5(File file1,File file2){
String md5_1= FileMd5Util.getFileMD5(file1);
String md5_2= FileMd5Util.getFileMD5(file2);
return md5_1.equals(md5_2);
}
/***
* compare two file by Md5
*
* @param filepath1
* @param filepath2
* @return
*/
public static boolean isSameMd5(String filepath1,String filepath2){
File file1=new File(filepath1);
File file2=new File(filepath2);
return isSameMd5(file1, file2);
}
}
2、文件名生成工具。
public class NameUtil {
/**
* Java文件操作 获取文件扩展名
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename.toLowerCase();
}
/**
* Java文件操作 获取不带扩展名的文件名
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename.toLowerCase();
}
public static void main(String[] args) {
String str = "AAbbbCC.jpg";
System.out.println(getExtensionName(str).toLowerCase());
System.out.println(getFileNameNoEx(str).toUpperCase());
}
}
删除文件工具类
package com.yin.freemakeradd.utils;
import java.io.File;
public class FileUtil {
/**
* 删除单个文件
* @param filePath 被删除文件的文件名
* @return 文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
/**
* 删除文件夹以及目录下的文件
* @param filePath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String filePath) {
boolean flag = false;
//如果filePath不以文件分隔符结尾,自动添加文件分隔符
if (!filePath.endsWith(File.separator)) {
filePath = filePath + File.separator;
}
File dirFile = new File(filePath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
File[] files = dirFile.listFiles();
//遍历删除文件夹下的所有文件(包括子目录)
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
//删除子文件
flag = deleteFile(files[i].getAbsolutePath());
if (!flag){
break;
}
} else {
//删除子目录
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag){
break;
}
}
}
if (!flag){
return false;
}
//删除当前空目录
return dirFile.delete();
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
* @param filePath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean deleteFolder(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return false;
} else {
if (file.isFile()) {
// 为文件时调用删除文件方法
return deleteFile(filePath);
} else {
// 为目录时调用删除目录方法
return deleteDirectory(filePath);
}
}
}
public static void main(String[] args) {
deleteFolder("C:\\Users\\Administrator\\Desktop\\upload"+"\\upload" + File.separator + "20200118"+File.separator+"eed4e9e3-52f0-4755-a391-aaba54d606cc");
}
}
前端引入md5.js文件
/* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}
/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> 5] |= 0x80 << ((len) % 32);
x[(((len + 64) >>> 9) << 4) + 14] = len;
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
for(var i = 0; i < x.length; i += 16)
{
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
}
return Array(a, b, c, d);
}
/*
* These functions implement the four basic operations the algorithm uses.
*/
function md5_cmn(q, a, b, x, s, t)
{
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
/*
* Calculate the HMAC-MD5, of a key and some data
*/
function core_hmac_md5(key, data)
{
var bkey = str2binl(key);
if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
return core_md5(opad.concat(hash), 512 + 128);
}
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y)
{
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
function bit_rol(num, cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}
/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i < binarray.length * 4; i++)
{
str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for(var i = 0; i < binarray.length * 4; i += 3)
{
var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++)
{
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}