小字符串的快速开源校验和

2023-10-01前端开发问题
7

本文介绍了小字符串的快速开源校验和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

对于小字符串(20-500 个字符),我需要一个快速校验和(尽可能快).

I need a quick checksum (as fast as possilbe) for small strings (20-500 chars).

我需要源代码,而且它必须很小!(最大约 100 LOC)

I need the source code and that must be small! (about 100 LOC max)

如果它可以在 Base32/64 中生成字符串.(或类似的东西)这将是完美的.基本上校验和不能使用任何坏"字符..你知道..通常的 (){}[].,;:/+-|等等

If it could generate strings in Base32/64. (or something similar) it would be perfect. Basically the checksums cannot use any "bad" chars.. you know.. the usual (){}[].,;:/+-| etc

澄清

它可能是强/弱,这并不重要,因为它仅用于幕后目的.

It could be strong/weak, that really doesn't matter since it is only for behind-the-scenes purposes.

它不需要包含原始字符串的所有数据,因为我只会与生成的校验和进行比较,我不希望有任何解密".

It need not contain all the data of the original string since I will be only doing comparison with generated checksums, I don't expect any sort of "decryption".

推荐答案

用C快速实现,我这边没有版权,随便用吧.但请注意,这是一个非常弱的校验和",所以不要将它用于严肃的事情:) - 但这就是你想要的,不是吗?

Quick implementation in C, no copyrights from my side, so use it as you wish. But please note that this is a very weak "checksum", so don't use it for serious things :) - but that's what you wanted, isn't it?

这将返回一个 32 位整数校验和,编码为包含其十六进制值的字符串.如果校验和函数不能满足您的需求,您可以将 chk += ((int)(str[i]) * (i + 1)); 行更改为更好的(fe 乘法),加法和按位旋转会更好).

This returns an 32-bit integer checksum encoded as an string containing its hex value. If the checksum function doesn't satisfy your needs, you can change the chk += ((int)(str[i]) * (i + 1)); line to something better (f.e. multiplication, addition and bitwise rotating would be much better).

按照hughdbrown的建议和他链接的答案之一,我更改了for 循环,因此它不会在每次迭代时调用 strlen.

Following hughdbrown's advice and one of the answers he linked, I changed the for loop so it doesn't call strlen with every iteration.

#include <stdio.h>
#include <stdlib.h>
#include <string>

char* hextab = "0123456789ABCDEF";

char* encode_int(int i) {
  char* c = (char*)malloc(sizeof(char) * 9);

  for (int j = 0; j < 4; j++) {
    c[(j << 1)] = hextab[((i % 256) >> 4)];
    c[(j << 1) + 1] = hextab[((i % 256) % 16)];

    i = (i >> 8);
  }
  c[8] = 0;

  return c;
}

int checksum(char* str) {
  int i;
  int chk = 0x12345678;

  for (i = 0; str[i] != ''; i++) {
    chk += ((int)(str[i]) * (i + 1));
  }

  return chk;
}

int main() {
  char* str1 = "Teststring";
  char* str2 = "Teststring2";

  printf("string: %s, checksum string: %s
", str1, encode_int(checksum(str1)));
  printf("string: %s, checksum string: %s
", str2, encode_int(checksum(str2)));

  return 0;
}

这篇关于小字符串的快速开源校验和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

layui树状组件tree怎么默认勾选?
在layui树状组件tree中,勾选问题可以通过以下方法解决: 通过tree的oncheck事件来监听勾选操作,然后根据勾选状态进行相应的处理。例如: tree.on('check', function(obj) { // 获取勾选状态 var isChecked = obj.checked; // 获取当前节点数据 var data =...
2024-11-09 前端开发问题
372

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5