How Do I Generate Random Identicons? [closed] - html
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am looking for the most basic way to generate random identicons with no regards to information such as a user email or IP address etc.
I wrote this the other day: http://jsfiddle.net/pHv6W/
It will generate a random hexadecimal hash and display an identicon and a monsterID from gravatar.com
// Load the MD5 script
MD5 = makeMD5();
seed = randomString();
image = document.getElementById('image');
image2 = document.getElementById('image2');
hash = MD5.hex(seed);
image.src = 'http://www.gravatar.com/avatar/' + hash + '?d=identicon&s=150';
image2.src = 'http://www.gravatar.com/avatar/' + hash + '?d=monsterid&s=150';
function randomString(length) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
if (!length) {
length = Math.floor(Math.random() * chars.length);
}
var str = '';
for (var i = 0; i < length; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
/*
* 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.
*/
function makeMD5() {
/*
* 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 [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 * MD5.chrsz);
var ipad = [],
opad = [];
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 * MD5.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 = [],
chrsz = MD5.chrsz;
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 = "",
chrsz = MD5.chrsz;
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 = MD5.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 += MD5.b64pad;
else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
}
}
return str;
}
return {
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
hexcase: 0, // hex output format. 0 - lowercase; 1 - uppercase
b64pad: "", // base-64 pad character. "=" for strict RFC compliance
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
*/
hex: function(s) {
return binl2hex(core_md5(str2binl(s), s.length * MD5.chrsz));
},
base64: function(s) {
return binl2b64(core_md5(str2binl(s), s.length * MD5.chrsz));
},
string: function(s) {
return binl2str(core_md5(str2binl(s), s.length * MD5.chrsz));
},
hmac: {
hex: function(key, data) {
return binl2hex(core_hmac_md5(key, data));
},
base64: function(key, data) {
return binl2b64(core_hmac_md5(key, data));
},
string: function(key, data) {
return binl2str(core_hmac_md5(key, data));
}
},
test: function() { // Perform a simple self-test to see if the VM is working
return this.hex("abc") == "900150983cd24fb0d6963f7d28e17f72";
}
};
}
<img id="image">
<img id="image2">
I made a jQuery plugin to solve this problem: You just need to supply the hash value.
Demo : http://francisshanahan.com/identicon5/test.html
jQuery Page: http://plugins.jquery.com/project/identicon5
Project Page: http://francisshanahan.com/index.php/identicon5
Wikipedia lists a number of implementations that allow this:
http://en.wikipedia.org/wiki/Identicon#Implementations
You could seed a library call with whatever random data you want.
Related
my function is invalid and fzero can't handle it
I get this error error: invalid function handle, unable to find function for #0.1*x - sin(2 * x) + 0.25 error: called from fzero at line 178 column 6 lab6 at line 82 column 3 Part of code where it happens is: f= '0.1*x - sin(2 * x) + 0.25'; [a, b] = fgraf(f, -3, 3); fzero(f, [a, b]); zs1 = ans; [a, b] = fgraf(f, -3, 3); zs2 = fzero(f, [a, b]); [a, b] = fgraf(f, -3, 3); zs3 = fzero(f, [a, b]);
fgraf doesn’t seem to be a standard MATLAB function, so I can’t be sure, but it looks like it expects a function handle as input. Define f as f = #(x) 0.1*x - sin(2 * x) + 0.25;
Dart Function.appy() does not work on Color.fromARGB(a, r, g, b);
Experimenting with Function.apply() to pass a list of positional parameters. But why doesn't a constructor method work for class Color? var x = Function.apply(Color.fromARGB, [255, 66, 165, 245]); Error Message: The getter 'fromARGB' isn't defined for the class 'Color'. Here is the constructor. const Color.fromARGB(int a, int r, int g, int b) : value = (((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | ((b & 0xff) << 0)) & 0xFFFFFFFF;
A named constructor is not a Function in Dart. The current way to write your sample is: var x = Function.apply( (int a, int r, int g, int b) => Color.fromARGB(a, r, g, b), [255, 66, 165, 245]); https://github.com/dart-lang/language/issues/216 is the canonical issue to track requesting support for "tearing off" a constructor to treat it as a Function.
How to Convert Hexdecimal code to color in windows 8
I have code Hexadecimal code "FFB800" and I needed to convert to "Color" in WinRT. Thanks in Advance.
What is the purpose of the question? Is it an option to do this in plain XAML? XAML does take Hexadecimal codes. <Grid Background="#FFB800"> Otherwise in code-behind I've used more or less the following in a Windows 8 App: var hexCode = "#FFFFB800"; var color = new Color(); color.A = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier); color.R = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier); color.G = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier); color.B = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);
The short way to do it in a tweet: (Color)XamlReader.Load(string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\">{0}</Color>", c)); The recommended way is to get WinRT XAML Toolkit from NuGet and call WinRTXamlToolkit.Imaging.ColorExtensions.FromString(c); This runs way faster than using XamlReader, so it is recommended if you need to call it more than once. You can also clone it from GitHub or copy and paste from here: #region FromString() /// <summary> /// Returns a Color based on XAML color string. /// </summary> /// <param name="c">The color string. Any format used in XAML should work.</param> /// <returns></returns> public static Color FromString(string c) { if (string.IsNullOrEmpty(c)) throw new ArgumentException("Invalid color string.", "c"); if (c[0] == '#') { switch (c.Length) { case 9: { //var cuint = uint.Parse(c.Substring(1), NumberStyles.HexNumber); var cuint = Convert.ToUInt32(c.Substring(1), 16); var a = (byte)(cuint >> 24); var r = (byte)((cuint >> 16) & 0xff); var g = (byte)((cuint >> 8) & 0xff); var b = (byte)(cuint & 0xff); return Color.FromArgb(a, r, g, b); } case 7: { var cuint = Convert.ToUInt32(c.Substring(1), 16); var r = (byte)((cuint >> 16) & 0xff); var g = (byte)((cuint >> 8) & 0xff); var b = (byte)(cuint & 0xff); return Color.FromArgb(255, r, g, b); } case 5: { var cuint = Convert.ToUInt16(c.Substring(1), 16); var a = (byte)(cuint >> 12); var r = (byte)((cuint >> 8) & 0xf); var g = (byte)((cuint >> 4) & 0xf); var b = (byte)(cuint & 0xf); a = (byte)(a << 4 | a); r = (byte)(r << 4 | r); g = (byte)(g << 4 | g); b = (byte)(b << 4 | b); return Color.FromArgb(a, r, g, b); } case 4: { var cuint = Convert.ToUInt16(c.Substring(1), 16); var r = (byte)((cuint >> 8) & 0xf); var g = (byte)((cuint >> 4) & 0xf); var b = (byte)(cuint & 0xf); r = (byte)(r << 4 | r); g = (byte)(g << 4 | g); b = (byte)(b << 4 | b); return Color.FromArgb(255, r, g, b); } default: throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", c)); } } else if ( c.Length > 3 && c[0] == 's' && c[1] == 'c' && c[2] == '#') { var values = c.Split(','); if (values.Length == 4) { var scA = double.Parse(values[0].Substring(3)); var scR = double.Parse(values[1]); var scG = double.Parse(values[2]); var scB = double.Parse(values[3]); return Color.FromArgb( (byte)(scA * 255), (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } else if (values.Length == 3) { var scR = double.Parse(values[0].Substring(3)); var scG = double.Parse(values[1]); var scB = double.Parse(values[2]); return Color.FromArgb( 255, (byte)(scR * 255), (byte)(scG * 255), (byte)(scB * 255)); } else { throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", c)); } } else { var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(c); return (Color)prop.GetValue(null); } } #endregion
var hexCode = "#FFFFB800"; var color = new Color(); color.A = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier); color.R = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier); color.G = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier); color.B = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier); how to set as fill for a xaml rectangle object rect.Fill = new SolidColorBrush(color); the other solution like this one works but returns the Parameters out of order if you have only a 6 digit hex rather than the full 8 simply set the a to 255
Try this: public struct MyColor : Windows.UI.Color { /// <summary> /// Convert hexdecimal value into color. /// </summary> /// <param name="hexCode">hexdecimal of color.</param> /// <returns></returns> public Windows.UI.Xaml.Media.Brush ColorToBrush(string hexCode) { hexCode = hexCode.Replace("#", ""); if (hexCode.Length == 6) return new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255, byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.HexNumber), byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.HexNumber), byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.HexNumber))); else if (hexCode.Length == 8) { var color = new Windows.UI.Color(); color.A = byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier); color.R = byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier); color.G = byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier); color.B = byte.Parse(hexCode.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier); } return null; } }
Convert a list of commands into a function
I have made this f[x_] := x - 2 x0 = 999.; imax = 5; Module[{i, x}, x[0] = x0; For[i = 0, i < imax, x[i + 1] = x[i] - f[x[i]]/f'[x[i]]; Print[x[i]]; i++]] and am trying to turn this into a newton rhapson function. I need to be able to input the Function F[x],an initial guess,and imax.
A little bit more Mathematica-ish: newt[f_, x0_, imax_] := NestList[# - f##/f'## &, x0, imax]; f[x_] := x - 2 x0 = 999; imax = 5; newt[f, x0, imax] (* -> {999, 2, 2, 2, 2, 2} *)
Mathematica is that simple: newtonraph = Function[{f,x0,imax},Module[{i,x}, x[0] = x0; For[i=0, i < imax, x[i+1] = x[i] - f[x[i]]/f'[x[i]]; Print[x[i]]; i++ ]; ]; ]; and call the function: func[t_] = 23 + t + 2*(t^2) newtonraph[func,10,100]
Calculating MD5 of string from Microsoft Access
How do I calculate an MD5 of a string client side in Microsoft Access?
If you are implementing MD5 through a VBA module then there are several options. I haven't tried this, but a quick search for "md5 vba" returned this basMD5.bas file. It is not one of the common open source licenses though. From the "MD5 Homepage (unofficial)" in the Visual Basic section: Francisco Carlos Piragibe de Almeida has created a DLL with a BAS wrapper module for use in VB projects (19K download) and an ActiveX in-process server wrapper around the original DLL (includes the DLL) (1394K download). (appears to be broken -- 2005-11-14) MD5 for VB. Reference by benjamin pritchard. MD5 for VB and VBScript. Reference by Dominik Deobald.
From this wikibook Public Function Hash_MD5(ByVal sIn As String) As String 'Set a reference to mscorlib 4.0 64-bit Dim oT As Object, oMD5 As Object Dim TextToHash() As Byte Dim bytes() As Byte Set oT = CreateObject("System.Text.UTF8Encoding") Set oMD5 = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider") TextToHash = oT.GetBytes_4(sIn) bytes = oMD5.ComputeHash_2((TextToHash)) Hash_MD5 = ConvToHexString(bytes) Set oT = Nothing Set oMD5 = Nothing End Function Private Function ConvToHexString(ByVal vIn As Variant) As Variant Dim oD As Object Set oD = CreateObject("MSXML2.DOMDocument") With oD .LoadXML "<root />" .DocumentElement.DataType = "bin.Hex" .DocumentElement.nodeTypedValue = vIn End With ConvToHexString = Replace(oD.DocumentElement.text, vbLf, "") Set oD = Nothing End Function
Looks like D.W. Fenton asked the same thing a few years ago on bytes.com Here is where he found the solution Here's the relevant code from the ASP example listed there, this should be pretty close to the VBA: ' Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm, ' as set out in the memo RFC1321. ' ' See the VB6 project that accompanies this sample for full code comments on how ' it works. ' ' ASP VBScript code for generating an MD5 'digest' or 'signature' of a string. The ' MD5 algorithm is one of the industry standard methods for generating digital ' signatures. It is generically known as a digest, digital signature, one-way ' encryption, hash or checksum algorithm. A common use for MD5 is for password ' encryption as it is one-way in nature, that does not mean that your passwords ' are not free from a dictionary attack. ' ' This is 'free' software with the following restrictions: ' ' You may not redistribute this code as a 'sample' or 'demo'. However, you are free ' to use the source code in your own code, but you may not claim that you created ' the sample code. It is expressly forbidden to sell or profit from this source code ' other than by the knowledge gained or the enhanced value added by your own code. ' ' Use of this software is also done so at your own risk. The code is supplied as ' is without warranty or guarantee of any kind. ' ' Should you wish to commission some derivative work based on this code provided ' here, or any consultancy work, please do not hesitate to contact us. ' ' Web Site: http://www.frez.co.uk ' E-mail: sales#frez.co.uk Private Const BITS_TO_A_BYTE = 8 Private Const BYTES_TO_A_WORD = 4 Private Const BITS_TO_A_WORD = 32 Private m_lOnBits(30) Private m_l2Power(30) m_lOnBits(0) = CLng(1) m_lOnBits(1) = CLng(3) m_lOnBits(2) = CLng(7) m_lOnBits(3) = CLng(15) m_lOnBits(4) = CLng(31) m_lOnBits(5) = CLng(63) m_lOnBits(6) = CLng(127) m_lOnBits(7) = CLng(255) m_lOnBits(8) = CLng(511) m_lOnBits(9) = CLng(1023) m_lOnBits(10) = CLng(2047) m_lOnBits(11) = CLng(4095) m_lOnBits(12) = CLng(8191) m_lOnBits(13) = CLng(16383) m_lOnBits(14) = CLng(32767) m_lOnBits(15) = CLng(65535) m_lOnBits(16) = CLng(131071) m_lOnBits(17) = CLng(262143) m_lOnBits(18) = CLng(524287) m_lOnBits(19) = CLng(1048575) m_lOnBits(20) = CLng(2097151) m_lOnBits(21) = CLng(4194303) m_lOnBits(22) = CLng(8388607) m_lOnBits(23) = CLng(16777215) m_lOnBits(24) = CLng(33554431) m_lOnBits(25) = CLng(67108863) m_lOnBits(26) = CLng(134217727) m_lOnBits(27) = CLng(268435455) m_lOnBits(28) = CLng(536870911) m_lOnBits(29) = CLng(1073741823) m_lOnBits(30) = CLng(2147483647) m_l2Power(0) = CLng(1) m_l2Power(1) = CLng(2) m_l2Power(2) = CLng(4) m_l2Power(3) = CLng(8) m_l2Power(4) = CLng(16) m_l2Power(5) = CLng(32) m_l2Power(6) = CLng(64) m_l2Power(7) = CLng(128) m_l2Power(8) = CLng(256) m_l2Power(9) = CLng(512) m_l2Power(10) = CLng(1024) m_l2Power(11) = CLng(2048) m_l2Power(12) = CLng(4096) m_l2Power(13) = CLng(8192) m_l2Power(14) = CLng(16384) m_l2Power(15) = CLng(32768) m_l2Power(16) = CLng(65536) m_l2Power(17) = CLng(131072) m_l2Power(18) = CLng(262144) m_l2Power(19) = CLng(524288) m_l2Power(20) = CLng(1048576) m_l2Power(21) = CLng(2097152) m_l2Power(22) = CLng(4194304) m_l2Power(23) = CLng(8388608) m_l2Power(24) = CLng(16777216) m_l2Power(25) = CLng(33554432) m_l2Power(26) = CLng(67108864) m_l2Power(27) = CLng(134217728) m_l2Power(28) = CLng(268435456) m_l2Power(29) = CLng(536870912) m_l2Power(30) = CLng(1073741824) Private Function LShift(lValue, iShiftBits) If iShiftBits = 0 Then LShift = lValue Exit Function ElseIf iShiftBits = 31 Then If lValue And 1 Then LShift = &H80000000 Else LShift = 0 End If Exit Function ElseIf iShiftBits < 0 Or iShiftBits > 31 Then Err.Raise 6 End If If (lValue And m_l2Power(31 - iShiftBits)) Then LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * m_l2Power(iShiftBits)) Or &H80000000 Else LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * m_l2Power(iShiftBits)) End If End Function Private Function RShift(lValue, iShiftBits) If iShiftBits = 0 Then RShift = lValue Exit Function ElseIf iShiftBits = 31 Then If lValue And &H80000000 Then RShift = 1 Else RShift = 0 End If Exit Function ElseIf iShiftBits < 0 Or iShiftBits > 31 Then Err.Raise 6 End If RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits) If (lValue And &H80000000) Then RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1))) End If End Function Private Function RotateLeft(lValue, iShiftBits) RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits)) End Function Private Function AddUnsigned(lX, lY) Dim lX4 Dim lY4 Dim lX8 Dim lY8 Dim lResult lX8 = lX And &H80000000 lY8 = lY And &H80000000 lX4 = lX And &H40000000 lY4 = lY And &H40000000 lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF) If lX4 And lY4 Then lResult = lResult Xor &H80000000 Xor lX8 Xor lY8 ElseIf lX4 Or lY4 Then If lResult And &H40000000 Then lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8 Else lResult = lResult Xor &H40000000 Xor lX8 Xor lY8 End If Else lResult = lResult Xor lX8 Xor lY8 End If AddUnsigned = lResult End Function Private Function F(x, y, z) F = (x And y) Or ((Not x) And z) End Function Private Function G(x, y, z) G = (x And z) Or (y And (Not z)) End Function Private Function H(x, y, z) H = (x Xor y Xor z) End Function Private Function I(x, y, z) I = (y Xor (x Or (Not z))) End Function Private Sub FF(a, b, c, d, x, s, ac) a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac)) a = RotateLeft(a, s) a = AddUnsigned(a, b) End Sub Private Sub GG(a, b, c, d, x, s, ac) a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac)) a = RotateLeft(a, s) a = AddUnsigned(a, b) End Sub Private Sub HH(a, b, c, d, x, s, ac) a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac)) a = RotateLeft(a, s) a = AddUnsigned(a, b) End Sub Private Sub II(a, b, c, d, x, s, ac) a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac)) a = RotateLeft(a, s) a = AddUnsigned(a, b) End Sub Private Function ConvertToWordArray(sMessage) Dim lMessageLength Dim lNumberOfWords Dim lWordArray() Dim lBytePosition Dim lByteCount Dim lWordCount Const MODULUS_BITS = 512 Const CONGRUENT_BITS = 448 lMessageLength = Len(sMessage) lNumberOfWords = (((lMessageLength + ((MODULUS_BITS - CONGRUENT_BITS) \ BITS_TO_A_BYTE)) \ (MODULUS_BITS \ BITS_TO_A_BYTE)) + 1) * (MODULUS_BITS \ BITS_TO_A_WORD) ReDim lWordArray(lNumberOfWords - 1) lBytePosition = 0 lByteCount = 0 Do Until lByteCount >= lMessageLength lWordCount = lByteCount \ BYTES_TO_A_WORD lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(Asc(Mid(sMessage, lByteCount + 1, 1)), lBytePosition) lByteCount = lByteCount + 1 Loop lWordCount = lByteCount \ BYTES_TO_A_WORD lBytePosition = (lByteCount Mod BYTES_TO_A_WORD) * BITS_TO_A_BYTE lWordArray(lWordCount) = lWordArray(lWordCount) Or LShift(&H80, lBytePosition) lWordArray(lNumberOfWords - 2) = LShift(lMessageLength, 3) lWordArray(lNumberOfWords - 1) = RShift(lMessageLength, 29) ConvertToWordArray = lWordArray End Function Private Function WordToHex(lValue) Dim lByte Dim lCount For lCount = 0 To 3 lByte = RShift(lValue, lCount * BITS_TO_A_BYTE) And m_lOnBits(BITS_TO_A_BYTE - 1) WordToHex = WordToHex & Right("0" & Hex(lByte), 2) Next End Function Public Function MD5(sMessage) Dim x Dim k Dim AA Dim BB Dim CC Dim DD Dim a Dim b Dim c Dim d Const S11 = 7 Const S12 = 12 Const S13 = 17 Const S14 = 22 Const S21 = 5 Const S22 = 9 Const S23 = 14 Const S24 = 20 Const S31 = 4 Const S32 = 11 Const S33 = 16 Const S34 = 23 Const S41 = 6 Const S42 = 10 Const S43 = 15 Const S44 = 21 x = ConvertToWordArray(sMessage) a = &H67452301 b = &HEFCDAB89 c = &H98BADCFE d = &H10325476 For k = 0 To UBound(x) Step 16 AA = a BB = b CC = c DD = d FF a, b, c, d, x(k + 0), S11, &HD76AA478 FF d, a, b, c, x(k + 1), S12, &HE8C7B756 FF c, d, a, b, x(k + 2), S13, &H242070DB FF b, c, d, a, x(k + 3), S14, &HC1BDCEEE FF a, b, c, d, x(k + 4), S11, &HF57C0FAF FF d, a, b, c, x(k + 5), S12, &H4787C62A FF c, d, a, b, x(k + 6), S13, &HA8304613 FF b, c, d, a, x(k + 7), S14, &HFD469501 FF a, b, c, d, x(k + 8), S11, &H698098D8 FF d, a, b, c, x(k + 9), S12, &H8B44F7AF FF c, d, a, b, x(k + 10), S13, &HFFFF5BB1 FF b, c, d, a, x(k + 11), S14, &H895CD7BE FF a, b, c, d, x(k + 12), S11, &H6B901122 FF d, a, b, c, x(k + 13), S12, &HFD987193 FF c, d, a, b, x(k + 14), S13, &HA679438E FF b, c, d, a, x(k + 15), S14, &H49B40821 GG a, b, c, d, x(k + 1), S21, &HF61E2562 GG d, a, b, c, x(k + 6), S22, &HC040B340 GG c, d, a, b, x(k + 11), S23, &H265E5A51 GG b, c, d, a, x(k + 0), S24, &HE9B6C7AA GG a, b, c, d, x(k + 5), S21, &HD62F105D GG d, a, b, c, x(k + 10), S22, &H2441453 GG c, d, a, b, x(k + 15), S23, &HD8A1E681 GG b, c, d, a, x(k + 4), S24, &HE7D3FBC8 GG a, b, c, d, x(k + 9), S21, &H21E1CDE6 GG d, a, b, c, x(k + 14), S22, &HC33707D6 GG c, d, a, b, x(k + 3), S23, &HF4D50D87 GG b, c, d, a, x(k + 8), S24, &H455A14ED GG a, b, c, d, x(k + 13), S21, &HA9E3E905 GG d, a, b, c, x(k + 2), S22, &HFCEFA3F8 GG c, d, a, b, x(k + 7), S23, &H676F02D9 GG b, c, d, a, x(k + 12), S24, &H8D2A4C8A HH a, b, c, d, x(k + 5), S31, &HFFFA3942 HH d, a, b, c, x(k + 8), S32, &H8771F681 HH c, d, a, b, x(k + 11), S33, &H6D9D6122 HH b, c, d, a, x(k + 14), S34, &HFDE5380C HH a, b, c, d, x(k + 1), S31, &HA4BEEA44 HH d, a, b, c, x(k + 4), S32, &H4BDECFA9 HH c, d, a, b, x(k + 7), S33, &HF6BB4B60 HH b, c, d, a, x(k + 10), S34, &HBEBFBC70 HH a, b, c, d, x(k + 13), S31, &H289B7EC6 HH d, a, b, c, x(k + 0), S32, &HEAA127FA HH c, d, a, b, x(k + 3), S33, &HD4EF3085 HH b, c, d, a, x(k + 6), S34, &H4881D05 HH a, b, c, d, x(k + 9), S31, &HD9D4D039 HH d, a, b, c, x(k + 12), S32, &HE6DB99E5 HH c, d, a, b, x(k + 15), S33, &H1FA27CF8 HH b, c, d, a, x(k + 2), S34, &HC4AC5665 II a, b, c, d, x(k + 0), S41, &HF4292244 II d, a, b, c, x(k + 7), S42, &H432AFF97 II c, d, a, b, x(k + 14), S43, &HAB9423A7 II b, c, d, a, x(k + 5), S44, &HFC93A039 II a, b, c, d, x(k + 12), S41, &H655B59C3 II d, a, b, c, x(k + 3), S42, &H8F0CCC92 II c, d, a, b, x(k + 10), S43, &HFFEFF47D II b, c, d, a, x(k + 1), S44, &H85845DD1 II a, b, c, d, x(k + 8), S41, &H6FA87E4F II d, a, b, c, x(k + 15), S42, &HFE2CE6E0 II c, d, a, b, x(k + 6), S43, &HA3014314 II b, c, d, a, x(k + 13), S44, &H4E0811A1 II a, b, c, d, x(k + 4), S41, &HF7537E82 II d, a, b, c, x(k + 11), S42, &HBD3AF235 II c, d, a, b, x(k + 2), S43, &H2AD7D2BB II b, c, d, a, x(k + 9), S44, &HEB86D391 a = AddUnsigned(a, AA) b = AddUnsigned(b, BB) c = AddUnsigned(c, CC) d = AddUnsigned(d, DD) Next MD5 = LCase(WordToHex(a) & WordToHex(b) & WordToHex(c) & WordToHex(d)) End Function
As for my knowledge there is no builtin function in Access for calculating MD5 hashes. You need either an external API (as mentioned by casperOne) or you could use an own implementation of MD5 in VBA. Googling for it I came up with this module, which you could add to you project, but I cannot guarantee for anything.
You could install a freeware COM component like this one and then use it from VBA
You can use the Cryptography API: Next Generation, the reference for which is found here: http://msdn.microsoft.com/en-us/library/aa376210(VS.85).aspx It might be a little difficult to use this through DECLARE statements in VBA (which you will have to use), so you can always use COM interop to call methods in .NET which access the MD5 class in the System.Security.Cryptography namespace.