Skip to content
This repository was archived by the owner on Mar 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions test/testMiddleState.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font: 13px mono;
}
.good {
color: green;
}
.bad {
color: red;
}
</style>
</head>
<body>
<script src="../yamd5.js"></script>
<script>
var tests = [

// from Appendix 5 of http://www.ietf.org/rfc/rfc1321.txt
'abc',
'message digest',
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
'12345678901234567890123456789012345678901234567890123456789012345678901234567890',

// Unicode strings taken from http://en.wikipedia.org/wiki/List_of_pangrams
// Then I ran them through md5sum
'Voix ambiguë d\'un cœur qui au zéphyr préfère les jattes de kiwis',
'Жълтата дюля беше щастлива, че пухът, който цъфна, замръзна като гьон',
'Hyvän lorun sangen pieneksi hyödyksi jäi suomen kirjaimet',
'Ταχίστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός',
'色は匂へど 散りぬるを 我が世誰ぞ 常ならむ 有為の奥山 今日越えて 浅き夢見じ 酔ひもせず',
'นายสังฆภัณฑ์ เฮงพิทักษ์ฝั่ง ผู้เฒ่าซึ่งมีอาชีพเป็นฅนขายฃวด ถูกตำรวจปฏิบัติการจับฟ้องศาล ฐานลักนาฬิกาคุณหญิงฉัตรชฎา ฌานสมาธิ'
];


var templateSuccess = '<span class="good">Success: "{{hash}}" === md5("{{data}}") === yamd5.setState(middleState).end()</span><br>';
var templateFailure = '<span class="bad">Failure: "{{hash}}" === md5("{{data}}") !== yamd5.setState(middleState).end()</span><br>';

var yamd5 = new YaMD5;

var middleState;

for(var i=0,len=tests.length; i<len; i++){
test(tests[i]);
}

function test(input){
var testStr = input;
for(var i=0,len=testStr.length; i<len; i++){
if (i !== 0){
yamd5.setState(middleState);
}else{
yamd5.start();
}
middleState = yamd5.appendStr(testStr.charAt(i)).getState();
}

var template;
if(yamd5.setState(middleState).end() === YaMD5.hashStr(testStr)){
template = templateSuccess;
}else{
template = templateFailure;
}
document.write(template.replace('{{data}}', testStr).replace('{{hash}}', YaMD5.hashStr(testStr)));
}


</script>



</body>
</html>
22 changes: 22 additions & 0 deletions yamd5.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ THE SOFTWARE.
return this;
};

MD5.prototype.getState = function() {
var middleState = {
state: this._state.buffer.slice(0),
dataLength: this._dataLength,
bufferLength: this._bufferLength
};
if (this._bufferLength) {
middleState.buffer = this._buffer.slice(0);
}
return middleState;
};

MD5.prototype.setState = function(state) {
this._state.set(new Int32Array(state.state));
this._dataLength = state.dataLength;
this._bufferLength = state.bufferLength;
if (state.bufferLength) {
this._buffer32.set(new Uint32Array(state.buffer));
}
return this;
};

MD5.prototype.start = function() {
this._dataLength = 0;
this._bufferLength = 0;
Expand Down
47 changes: 2 additions & 45 deletions yamd5.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.