{"id":36746395,"url":"https://github.com/cryptography-research-lab/go-cycle-string","last_synced_at":"2026-01-12T12:35:24.998Z","repository":{"id":65423918,"uuid":"587987906","full_name":"cryptography-research-lab/go-cycle-string","owner":"cryptography-research-lab","description":"循环字符串数据结构（CycleString）","archived":false,"fork":false,"pushed_at":"2023-01-29T16:43:09.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-31T22:51:49.703Z","etag":null,"topics":["cryptography","data-structures","utils-library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cryptography-research-lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-12T03:44:43.000Z","updated_at":"2023-01-25T11:41:22.000Z","dependencies_parsed_at":"2023-02-13T23:25:24.377Z","dependency_job_id":null,"html_url":"https://github.com/cryptography-research-lab/go-cycle-string","commit_stats":null,"previous_names":["golang-infrastructure/go-cycle-string"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/cryptography-research-lab/go-cycle-string","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptography-research-lab%2Fgo-cycle-string","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptography-research-lab%2Fgo-cycle-string/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptography-research-lab%2Fgo-cycle-string/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptography-research-lab%2Fgo-cycle-string/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cryptography-research-lab","download_url":"https://codeload.github.com/cryptography-research-lab/go-cycle-string/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptography-research-lab%2Fgo-cycle-string/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338979,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T12:22:26.515Z","status":"ssl_error","status_checked_at":"2026-01-12T12:22:10.856Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cryptography","data-structures","utils-library"],"created_at":"2026-01-12T12:35:24.318Z","updated_at":"2026-01-12T12:35:24.990Z","avatar_url":"https://github.com/cryptography-research-lab.png","language":"Go","readme":"# 循环字符串数据结构（CycleString）\n\n# 一、这是什么？解决了什么问题？\n\n这个库定义了一个名为`CycleString`的数据结构，其实就是基于一个有限长度的字符串进行无限次重复得到的一个长度无限的字符流，同时在这个数据结构的基础上提供了一些字符串操作方法以便将其当做一个正常的字符串使用。\n\n在研究古典密码学的时候发现一个很常见的场景就是秘钥补齐，比如要加密的文本的字符长度是20，但是加密使用的秘钥字符长度是10，则需要将秘钥重复两次凑齐20个字符与要加密的文本对齐再进行加密运算，于是就萌生了一个想法，干脆就定义一个特殊的字符串结构，这个字符串结构可以看做是一个基础字符串进行无限次重复而得到的一个长度无限的字符流，如此在加密场景下则可以忽略秘钥的长度让逻辑更清晰，从而将精力放在更重要的核心逻辑上而不是处理这些边边角角的鸡毛蒜皮，不止古典密码加密算法，其它类似的场景需求都可复用此工具库。\n\n# 二、安装\n\n```bash\ngo get -u github.com/cryptography-research-lab/go-cycle-string\n```\n\n# 三、示例代码\n\n## 3.1 创建一个无限循环字符串\n\n此数据结构需要基于一个有限长度的字符串，比如下面的代码就是创建一个无限长度的字符串：\n\n```go\ncycleString := cycle_string.NewCycleString(\"CC11001100\")\n```\n\n这个无限长度的字符串的前若干个字符是：\n\n```text\nCC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100CC11001100\n```\n\n## 3.2 字符串真实长度\n\n对于`CycleString`这种数据结构来说长度是没有意义的，因为它的长度是无穷，但是它所基于的字符串的长度是有限的，所以这里的真实长度都是指的它所基于的字符串的长度，这里的长度有两种，一种是字符长度，使用`RealRuneLength`来获取，一种是字节长度，使用`RealByteLength`来获取，下面是这两个方法的使用示例：\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tcycle_string \"github.com/cryptography-research-lab/go-cycle-string\"\n)\n\nfunc main() {\n\n\tcycleString := cycle_string.NewCycleString(\"CC中文\")\n\tfmt.Println(fmt.Sprintf(\"真实的字节数： %d\", cycleString.RealByteLength())) // Output: 真实的字节数： 8\n\tfmt.Println(fmt.Sprintf(\"真实的字符数： %d\", cycleString.RealRuneLength())) // Output: 真实的字符数： 4\n\n}\n```\n\n## 3.3 字符 \u0026 字节\n\n`CycleString`本质上是一个字符串，所以也可以按照下标获取对应位置的字符，对于下标的类型，又分为按照字节的下标和按照字符的下标：\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tcycle_string \"github.com/cryptography-research-lab/go-cycle-string\"\n)\n\nfunc main() {\n\n\tcycleString := cycle_string.NewCycleString(\"CC中文\")\n\n\t// 按照字节下标获取，这两个方法是等价的\n\tfmt.Println(\"At: \" + string(cycleString.At(2)))\n\tfmt.Println(\"ByteAt: \" + string(cycleString.ByteAt(2)))\n\n\t// 按照字符下标获取，这两个方法是等价的\n\tfmt.Println(\"RuneAt: \" + string(cycleString.RuneAt(2)))\n\tfmt.Println(\"CharAt: \" + string(cycleString.CharAt(2)))\n\n\t// Output:\n\t// At: ä\n\t// ByteAt: ä\n\t// RuneAt: 中\n\t// CharAt: 中\n\n}\n```\n\n## 3.4 子串 \n\n`CycleString`本质上是一个字符串，所以也可以获取其子串，获取子串有两种类型，一种是按字节长度获取，对应的方法名是`SubString`，一种是按字符数来获取，对应的方法是`SubStringRune`，使用哪个方法请按照自己的需求决定：\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tcycle_string \"github.com/cryptography-research-lab/go-cycle-string\"\n)\n\nfunc main() {\n\n\tcycleString := cycle_string.NewCycleString(\"中国\")\n\n\t// 按字节取子串，因为是中文，所以下面就乱码了\n\tsubString := cycleString.SubString(0, 1)\n\tfmt.Println(\"按字节数：\" + subString) // Output: 按字节数：�\n\n\t// 按字符取子串，则能够完整的取到一个中文字符 \n\tsubString = cycleString.SubStringRune(0, 1)\n\tfmt.Println(\"按字符数：\" + subString) // Output: 按字符数：中\n\n\t// 如果下标不合法的话则取到空字符串，不会panic\n\ts := cycleString.SubString(-1, 10)\n\tfmt.Println(\"越界： \" + s) // Output: 越界：\n\n}\n```\n\n## 3.5 迭代器模式\n\n`CycleString`有两个迭代器模式的实现`CycleStringByteIterator`和`CycleStringRuneIterator`，通过`ByteIterator`和`RuneIterator`方法来获取：\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\tcycle_string \"github.com/cryptography-research-lab/go-cycle-string\"\n\t\"time\"\n)\n\nfunc main() {\n\n\tcycleString := cycle_string.NewCycleString(\"CC11001100\")\n\titerator := cycleString.RuneIterator()\n\n\tfmt.Println(string(iterator.NextN(3)))\n\n\tfor iterator.Next() {\n\t\tfmt.Println(string(iterator.Value()))\n\t\ttime.Sleep(time.Millisecond * 100)\n\t}\n\t// Output:\n\t// CC1\n\t// 1\n\t// 0\n\t// 0\n\t// 1\n\t// 1\n\t// ...\n\n}\n```\n\n## 3.6 JSON序列化 \u0026 反序列化 \n\n此数据结构可以被正常的序列化和反序列化，只不过比较特殊的是因为`CycleString`这个数据结构本身来说是没有右边界的，而`JSON`序列化又需要它有一个明确的边界，所以`CycleString`重写了`JSON`序列化的`MarshalJSON`和`UnmarshalJSON`函数，在序列化的时候只保存`CycleString`所基于的字符串，下面的代码是一个`JSON`序列化的例子：\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\tcycle_string \"github.com/cryptography-research-lab/go-cycle-string\"\n)\n\n// Foo CycleString作为Foo这个Struct的一个字段，Foo可以被安全的JSON序列化\ntype Foo struct {\n\tBar *cycle_string.CycleString\n}\n\nfunc main() {\n\n\tfoo := Foo{\n\t\tBar: cycle_string.NewCycleString(\"CC11001100\"),\n\t}\n\tmarshal, err := json.Marshal(foo)\n\tif err != nil {\n\t\tfmt.Println(\"JSON序列化错误： \" + err.Error())\n\t\treturn\n\t}\n\tfmt.Println(\"序列化后的文本是： \" + string(marshal)) // Output: 序列化后的文本是： {\"Bar\":{\"CycleStringBaseString\":\"CC11001100\"}}\n\n\tfoo2 := \u0026Foo{}\n\terr = json.Unmarshal(marshal, foo2)\n\tif err != nil {\n\t\tfmt.Println(\"JSON反序列化错误： \" + err.Error())\n\t\treturn\n\t}\n\tfmt.Println(\"反序列后的前30个字符： \" + foo2.Bar.SubStringRune(0, 30)) // Output: 反序列后的前30个字符： CC11001100CC11001100CC11001100\n\n}\n```\n\n# 四、TODO \n\n暂未发现更多需求，如您使用了这个库又有相关功能当前未提供，可提`Issues`区讨论。\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptography-research-lab%2Fgo-cycle-string","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcryptography-research-lab%2Fgo-cycle-string","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptography-research-lab%2Fgo-cycle-string/lists"}