Skip to content

Commit 1613dfb

Browse files
committed
feat: add rest api checksum demo code
1 parent cb3b065 commit 1613dfb

File tree

14 files changed

+242
-0
lines changed

14 files changed

+242
-0
lines changed
Lines changed: 29 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 20 additions & 0 deletions
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+
}
Lines changed: 27 additions & 0 deletions
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+
Lines changed: 11 additions & 0 deletions
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+
}
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 24 additions & 0 deletions
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+
}
Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 12 additions & 0 deletions
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+
});

0 commit comments

Comments
 (0)