Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute a hash string using the specified digest algorithm on the specified value #8

Open
oshliaer opened this issue Aug 10, 2017 · 0 comments
Assignees
Labels

Comments

@oshliaer
Copy link
Owner

oshliaer commented Aug 10, 2017

Target

hash_('test', 'MD2');
> dd34716876364a02d0195e2fb9ae2d1b

hash_('');
> d41d8cd98f00b204e9800998ecf8427e

hash_('test');
> 098f6bcd4621d373cade4e832627b4f6

Snippet

/**
* Compute a hash string using the specified digest algorithm on the specified value.
* @param {String} value The specified value.
* @param {String} digestAlgorithm The name of Enum DigestAlgorithm: MD2, MD5, SHA_1, SHA_256, SHA_384, SHA_512
* @param {String} charset The name of Enum Charset: US_ASCII, UTF_8.
* @returns {String} The hash of value.
*/

function hash_(str, digestAlgorithm, charset) {
  charset = charset || Utilities.Charset.UTF_8;
  digestAlgorithm = digestAlgorithm || 'MD5';
  var digest = Utilities.computeDigest(Utilities.DigestAlgorithm[digestAlgorithm], str, charset);
  var __ = '';
  for (i = 0; i < digest.length; i++) {
    var byte = digest[i];
    if (byte < 0) byte += 256;
    var bStr = byte.toString(16);
    if (bStr.length == 1) bStr = '0' + bStr;
    __ += bStr;
  }
  return __;
}

Testing

/*
  Testing by GasT https://github.com/zixia/gast
*/

function test(){
  if ((typeof GasTap)==='undefined') { // GasT Initialization. (only if not initialized yet.)
    var cs = CacheService.getScriptCache().get('gast');
    if(!cs){
      cs = UrlFetchApp.fetch('https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js').getContentText();
      CacheService.getScriptCache().put('gast', cs, 21600);
    }
    eval(cs);
  } // Class GasTap is ready for use now!
  
  var test = new GasTap();
  
  test('Md5', function (t) {
    var i = hash_('');
    t.equal(i, 'd41d8cd98f00b204e9800998ecf8427e', '"" is d41d8cd98f00b204e9800998ecf8427e')
  });
  
  test('Md5', function (t) {
    var i = hash_('test', 'MD5');
    t.equal(i, '098f6bcd4621d373cade4e832627b4f6', '"" is 098f6bcd4621d373cade4e832627b4f6')
  });
  
  test('Md2', function (t) {
    var i = hash_('test', 'MD2');
    t.equal(i, 'dd34716876364a02d0195e2fb9ae2d1b', '"" is dd34716876364a02d0195e2fb9ae2d1b')
  });
  
  test('SHA_1', function (t) {
    var i = hash_('test', 'SHA_1');
    t.equal(i, 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3', '"" is a94a8fe5ccb19ba61c4c0873d391e987982fbbd3')
  });
  
  test('SHA_256', function (t) {
    var i = hash_('test', 'SHA_256');
    t.equal(i, '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', '"" is 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')
  });
  
  test('SHA_384', function (t) {
    var i = hash_('test', 'SHA_384');
    t.equal(i, '768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9', '"" is 768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9')
  });
  
  test('SHA_512', function (t) {
    var i = hash_('test', 'SHA_512');
    t.equal(i, 'ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff', '"" is ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff')
  });
  
  test.finish()
}
@oshliaer oshliaer self-assigned this Aug 10, 2017
oshliaer added a commit that referenced this issue Aug 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant