Forgot Password
 Register
View: 1582|Reply: 0

Create a MD5 function running on a google sheet

[Copy Link]
Posted on 2024-06-12 22:56:28 | Show all floors |Read Mode
  This is a normal MD5 hash, show the same result with md5hash in Oracle, md5 in mysql, md5sum in linux .
Open your Google Sheets --> Choose Extentions --> Apps  Script --> Code.gs and paste all the following code. Save it and close the project editor

  1. function MD5(input) {
  2.   // MD5 algorithm implementation
  3.   function md5cycle(x, k) {
  4.     var a = x[0], b = x[1], c = x[2], d = x[3];

  5.     a = ff(a, b, c, d, k[0], 7, -680876936);
  6.     d = ff(d, a, b, c, k[1], 12, -389564586);
  7.     c = ff(c, d, a, b, k[2], 17,  606105819);
  8.     b = ff(b, c, d, a, k[3], 22, -1044525330);
  9.     a = ff(a, b, c, d, k[4], 7, -176418897);
  10.     d = ff(d, a, b, c, k[5], 12,  1200080426);
  11.     c = ff(c, d, a, b, k[6], 17, -1473231341);
  12.     b = ff(b, c, d, a, k[7], 22, -45705983);
  13.     a = ff(a, b, c, d, k[8], 7,  1770035416);
  14.     d = ff(d, a, b, c, k[9], 12, -1958414417);
  15.     c = ff(c, d, a, b, k[10], 17, -42063);
  16.     b = ff(b, c, d, a, k[11], 22, -1990404162);
  17.     a = ff(a, b, c, d, k[12], 7,  1804603682);
  18.     d = ff(d, a, b, c, k[13], 12, -40341101);
  19.     c = ff(c, d, a, b, k[14], 17, -1502002290);
  20.     b = ff(b, c, d, a, k[15], 22,  1236535329);

  21.     a = gg(a, b, c, d, k[1], 5, -165796510);
  22.     d = gg(d, a, b, c, k[6], 9, -1069501632);
  23.     c = gg(c, d, a, b, k[11], 14,  643717713);
  24.     b = gg(b, c, d, a, k[0], 20, -373897302);
  25.     a = gg(a, b, c, d, k[5], 5, -701558691);
  26.     d = gg(d, a, b, c, k[10], 9,  38016083);
  27.     c = gg(c, d, a, b, k[15], 14, -660478335);
  28.     b = gg(b, c, d, a, k[4], 20, -405537848);
  29.     a = gg(a, b, c, d, k[9], 5,  568446438);
  30.     d = gg(d, a, b, c, k[14], 9, -1019803690);
  31.     c = gg(c, d, a, b, k[3], 14, -187363961);
  32.     b = gg(b, c, d, a, k[8], 20,  1163531501);
  33.     a = gg(a, b, c, d, k[13], 5, -1444681467);
  34.     d = gg(d, a, b, c, k[2], 9, -51403784);
  35.     c = gg(c, d, a, b, k[7], 14,  1735328473);
  36.     b = gg(b, c, d, a, k[12], 20, -1926607734);

  37.     a = hh(a, b, c, d, k[5], 4, -378558);
  38.     d = hh(d, a, b, c, k[8], 11, -2022574463);
  39.     c = hh(c, d, a, b, k[11], 16,  1839030562);
  40.     b = hh(b, c, d, a, k[14], 23, -35309556);
  41.     a = hh(a, b, c, d, k[1], 4, -1530992060);
  42.     d = hh(d, a, b, c, k[4], 11,  1272893353);
  43.     c = hh(c, d, a, b, k[7], 16, -155497632);
  44.     b = hh(b, c, d, a, k[10], 23, -1094730640);
  45.     a = hh(a, b, c, d, k[13], 4,  681279174);
  46.     d = hh(d, a, b, c, k[0], 11, -358537222);
  47.     c = hh(c, d, a, b, k[3], 16, -722521979);
  48.     b = hh(b, c, d, a, k[6], 23,  76029189);
  49.     a = hh(a, b, c, d, k[9], 4, -640364487);
  50.     d = hh(d, a, b, c, k[12], 11, -421815835);
  51.     c = hh(c, d, a, b, k[15], 16,  530742520);
  52.     b = hh(b, c, d, a, k[2], 23, -995338651);

  53.     a = ii(a, b, c, d, k[0], 6, -198630844);
  54.     d = ii(d, a, b, c, k[7], 10,  1126891415);
  55.     c = ii(c, d, a, b, k[14], 15, -1416354905);
  56.     b = ii(b, c, d, a, k[5], 21, -57434055);
  57.     a = ii(a, b, c, d, k[12], 6,  1700485571);
  58.     d = ii(d, a, b, c, k[3], 10, -1894986606);
  59.     c = ii(c, d, a, b, k[10], 15, -1051523);
  60.     b = ii(b, c, d, a, k[1], 21, -2054922799);
  61.     a = ii(a, b, c, d, k[8], 6,  1873313359);
  62.     d = ii(d, a, b, c, k[15], 10, -30611744);
  63.     c = ii(c, d, a, b, k[6], 15, -1560198380);
  64.     b = ii(b, c, d, a, k[13], 21,  1309151649);
  65.     a = ii(a, b, c, d, k[4], 6, -145523070);
  66.     d = ii(d, a, b, c, k[11], 10, -1120210379);
  67.     c = ii(c, d, a, b, k[2], 15,  718787259);
  68.     b = ii(b, c, d, a, k[9], 21, -343485551);

  69.     x[0] = add32(a, x[0]);
  70.     x[1] = add32(b, x[1]);
  71.     x[2] = add32(c, x[2]);
  72.     x[3] = add32(d, x[3]);
  73.   }

  74.   function cmn(q, a, b, x, s, t) {
  75.     a = add32(add32(a, q), add32(x, t));
  76.     return add32((a << s) | (a >>> (32 - s)), b);
  77.   }
  78.   function ff(a, b, c, d, x, s, t) {
  79.     return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  80.   }
  81.   function gg(a, b, c, d, x, s, t) {
  82.     return cmn((b & d) | (c & (~d)), a, b, x, s, t);
  83.   }
  84.   function hh(a, b, c, d, x, s, t) {
  85.     return cmn(b ^ c ^ d, a, b, x, s, t);
  86.   }
  87.   function ii(a, b, c, d, x, s, t) {
  88.     return cmn(c ^ (b | (~d)), a, b, x, s, t);
  89.   }

  90.   function md51(s) {
  91.     var txt = '',
  92.         n = s.length,
  93.         state = [1732584193, -271733879, -1732584194, 271733878],
  94.         i;

  95.     for (i = 64; i <= s.length; i += 64) {
  96.       md5cycle(state, md5blk(s.substring(i - 64, i)));
  97.     }
  98.     s = s.substring(i - 64);

  99.     var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  100.     for (i = 0; i < s.length; i++) {
  101.       tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
  102.     }
  103.     tail[i >> 2] |= 0x80 << ((i % 4) << 3);

  104.     if (i > 55) {
  105.       md5cycle(state, tail);
  106.       for (i = 0; i < 16; i++) {
  107.         tail[i] = 0;
  108.       }
  109.     }

  110.     tail[14] = n * 8;
  111.     md5cycle(state, tail);
  112.     return state;
  113.   }

  114.   function md5blk(s) {
  115.     var md5blks = [],
  116.         i;

  117.     for (i = 0; i < 64; i += 4) {
  118.       md5blks[i >> 2] = s.charCodeAt(i)
  119.                      + (s.charCodeAt(i + 1) << 8)
  120.                      + (s.charCodeAt(i + 2) << 16)
  121.                      + (s.charCodeAt(i + 3) << 24);
  122.     }
  123.     return md5blks;
  124.   }

  125.   var hex_chr = '0123456789abcdef'.split('');

  126.   function rhex(n) {
  127.     var s = '',
  128.         j = 0;
  129.     for (; j < 4; j++) {
  130.       s += hex_chr[(n >> (j * 8 + 4)) & 0x0F]
  131.          + hex_chr[(n >> (j * 8)) & 0x0F];
  132.     }
  133.     return s;
  134.   }

  135.   function hex(x) {
  136.     for (var i = 0; i < x.length; i++) {
  137.       x[i] = rhex(x[i]);
  138.     }
  139.     return x.join('');
  140.   }

  141.   function md5(s) {
  142.     return hex(md51(s));
  143.   }

  144.   // Utility function to add integers, handling wraparound
  145.   function add32(a, b) {
  146.     return (a + b) & 0xFFFFFFFF;
  147.   }

  148.   // Wrapper function for use in Google Sheets
  149.   return md5(input);
  150. }

  151. function onOpen() {
  152.   var ui = SpreadsheetApp.getUi();
  153.   ui.createMenu('MD5 Menu')
  154.     .addItem('MD5 Hash', 'showMD5Sidebar')
  155.     .addToUi();
  156. }

  157. function showMD5Sidebar() {
  158.   var html = HtmlService.createHtmlOutputFromFile('Page')
  159.       .setTitle('MD5 Hash Generator')
  160.       .setWidth(300);
  161.   SpreadsheetApp.getUi().showSidebar(html);
  162. }

  163. function getMD5(input) {
  164.   return MD5(input);
  165. }

Copy Code
In the cell that you want to calculate md5 value, just call a function getMD5("your text here") or getMD5(b2) ( b2 is in cell location that you want to get it encrypted.


Reply

Use Props Report

You need to log in before you can reply Login | Register

Forum Credit Rules

Close

Editors SelectedPrevious /2 Next

Archiver|Mobile|Darkroom|Privacy|DSC

GMT+7, 2024-12-03 23:41 , Processed in 0.012537 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

Quick Reply Back to Top Return to List