Skip to content

Commit 2e6d066

Browse files
committed
feat: add rest api checksum demo code
1 parent b765f1d commit 2e6d066

File tree

14 files changed

+242
-0
lines changed

14 files changed

+242
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <string>
4+
#include <sstream>
5+
#include <openssl/buffer.h>
6+
#include <cstring>
7+
#include <algorithm>
8+
#include <openssl/sha.h>
9+
#include <openssl/bio.h>
10+
#include <openssl/evp.h>
11+
12+
13+
std::string sha1(const std::string &input)
14+
{
15+
unsigned char hash[SHA_DIGEST_LENGTH];
16+
SHA1(reinterpret_cast<const unsigned char *>(input.c_str()), input.length(), hash);
17+
std::ostringstream oss;
18+
oss << std::hex << std::setfill('0');
19+
for (auto c : hash)
20+
{
21+
oss << std::setw(2) << static_cast<int>(c);
22+
}
23+
return oss.str();
24+
}
25+
26+
std::string getChecksum(const std::string appSecret, std::string nonce, long curtime)
27+
{
28+
return sha1(appSecret + nonce + std::to_string(curtime));
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../src/rest_api_auth.h"
2+
#include <gtest/gtest.h>
3+
#include <string>
4+
#include <stdint.h>
5+
6+
class RestApiAuth_test : public testing::Test
7+
{
8+
protected:
9+
virtual void SetUp() override {}
10+
11+
virtual void TearDown() {}
12+
13+
void TestGetChecksum()
14+
{
15+
std::string token = getChecksum("c00000000000", "1234567890", 1697168455);
16+
std::cout << "checksum: " << token << std::endl;
17+
EXPECT_EQ(token, "192bdbdad337836e6213aec1d93186aae9771c39");
18+
}
19+
};
20+
21+
TEST_F(RestApiAuth_test, RestApiAuth_test)
22+
{
23+
TestGetChecksum();
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using TokenBuilder;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace TokenBuilder.Tests
10+
{
11+
[TestClass()]
12+
public class RestApiAuthTests
13+
{
14+
[TestMethod()]
15+
public void GetChecksumTest()
16+
{
17+
Assert.AreEqual("192bdbdad337836e6213aec1d93186aae9771c39", RestApiAuth.GetChecksum("c00000000000", "1234567890", 1697168455));
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Security.Cryptography;
2+
using System.Text;
3+
4+
namespace TokenBuilder
5+
{
6+
public class RestApiAuth
7+
{
8+
public static string GetChecksum(string appSecret, string nonce, long curtime)
9+
{
10+
return ComputeSHA1($"{appSecret}{nonce}{curtime}");
11+
}
12+
private static string ComputeSHA1(string input)
13+
{
14+
using var sha1 = SHA1.Create();
15+
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
16+
var sb = new StringBuilder(hash.Length * 2);
17+
18+
foreach (byte b in hash)
19+
{
20+
sb.Append(b.ToString("x2"));
21+
}
22+
23+
return sb.ToString();
24+
}
25+
}
26+
}
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package token
2+
3+
import (
4+
"crypto/sha1"
5+
"fmt"
6+
)
7+
8+
func GetChecksum(appSecret, nonce string, curtime int64) string {
9+
raw := fmt.Sprintf("%s%s%d", appSecret, nonce, curtime)
10+
return fmt.Sprintf("%x", sha1.Sum([]byte(raw)))
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package token
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSecureChecksum(t *testing.T) {
10+
assert.Equal(t, "192bdbdad337836e6213aec1d93186aae9771c39",
11+
GetChecksum("c00000000000", "1234567890", 1697168455))
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.netease.im.rtctoken;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.security.MessageDigest;
5+
6+
public class RestApiAuth {
7+
public static String getChecksum(String appSecret, String nonce, int curTime) {
8+
return sha1(appSecret + nonce + curTime);
9+
}
10+
private static String sha1(String input) {
11+
try {
12+
MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
13+
byte[] result = mDigest.digest(input.getBytes(StandardCharsets.UTF_8));
14+
StringBuilder sb = new StringBuilder();
15+
for (byte b : result) {
16+
sb.append(String.format("%02x", b));
17+
}
18+
return sb.toString();
19+
} catch (Exception e) {
20+
throw new RuntimeException(e);
21+
}
22+
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.netease.im.rtctoken;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class RestApiAuthTest {
8+
@Test
9+
public void testChecksum() throws Exception {
10+
assertEquals("192bdbdad337836e6213aec1d93186aae9771c39", RestApiAuth.getChecksum("c00000000000", "1234567890", 1697168455));
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const crypto = require('crypto');
2+
3+
var GetChecksum = function (appSecret, nonce, curTime) {
4+
return sha1(`${appSecret}${nonce}${curTime}`);
5+
}
6+
7+
const sha1 = function (input) {
8+
const sha1 = crypto.createHash('sha1');
9+
sha1.update(input, 'utf8');
10+
return sha1.digest('hex');
11+
}
12+
13+
module.exports = {
14+
GetChecksum: GetChecksum,
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
4+
5+
const { GetChecksum } = require('../src/RestApiAuth.js');
6+
7+
test('GetChecksum', (t) => {
8+
var appSecret = "c00000000000";
9+
var nonce = "1234567890";
10+
var curTime = 1697168455;
11+
assert.equal("192bdbdad337836e6213aec1d93186aae9771c39", GetChecksum(appSecret, nonce, curTime));
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
function getChecksum(string $appSecret, string $nonce, int $curtime):string {
3+
return sha1($appSecret . $nonce . $curtime);
4+
}
5+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require_once "../src/RestApiAuth.php";
4+
5+
class ChecksumTest
6+
{
7+
public $appSecret = "c00000000000";
8+
9+
public function run()
10+
{
11+
$this->testChecksum();
12+
}
13+
14+
public function testChecksum()
15+
{
16+
$this->assertEqual(
17+
"192bdbdad337836e6213aec1d93186aae9771c39",
18+
getChecksum($this->appSecret, "1234567890", 1697168455)
19+
);
20+
}
21+
22+
public static function assertEqual($expected, $actual)
23+
{
24+
if ($expected != $actual) {
25+
echo "Assert failed" . "\n Expected :" . $expected . "\n Actual :" . $actual;
26+
} else {
27+
echo "Assert ok\n";
28+
}
29+
}
30+
}
31+
32+
$checksumTest = new ChecksumTest();
33+
$checksumTest->run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import hashlib
2+
3+
4+
def get_checksum(app_secret, nonce, timestamp):
5+
return hashlib.sha1(f'{app_secret}{nonce}{timestamp}'.encode()).hexdigest()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
from unittest.mock import patch
3+
import sys
4+
import os
5+
import hashlib
6+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
7+
from src import rest_api_auth
8+
9+
class TestGetChecksum(unittest.TestCase):
10+
def test_get_checksum(self):
11+
token = rest_api_auth.get_checksum("c00000000000", "1234567890", 1697168455)
12+
expected = "192bdbdad337836e6213aec1d93186aae9771c39"
13+
self.assertEqual(token, expected)

0 commit comments

Comments
 (0)