前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang语言情怀-第65期 Go 语言标准库翻译 crypto/rc4

Golang语言情怀-第65期 Go 语言标准库翻译 crypto/rc4

作者头像
李海彬
发布2021-03-09 11:04:52
5540
发布2021-03-09 11:04:52
举报
文章被收录于专栏:Golang语言社区Golang语言社区

import "crypto/rc4"

rc4包实现了RC4加密算法,

代码语言:javascript
复制
type KeySizeError
func (k KeySizeError) Error() string
type Cipher
func NewCipher(key []byte) (*Cipher, error)
func (c *Cipher) Reset()
func (c *Cipher) XORKeyStream(dst, src []byte)

type KeySizeError

代码语言:javascript
复制
type KeySizeError int
func (KeySizeError) Error
代码语言:javascript
复制
func (k KeySizeError) Error() string

type Cipher

代码语言:javascript
复制
type Cipher struct {
    // 内含隐藏或非导出字段
}

Cipher是一个使用特定密钥的RC4实例,本类型实现了cipher.Stream接口。

func NewCipher
代码语言:javascript
复制
func NewCipher(key []byte) (*Cipher, error)

NewCipher创建并返回一个新的Cipher。参数key是RC4密钥,至少1字节,最多256字节。

func (*Cipher) Reset
代码语言:javascript
复制
func (c *Cipher) Reset()

Reset方法会清空密钥数据,以便将其数据从程序内存中清除(以免被破解)

func (*Cipher) XORKeyStream
代码语言:javascript
复制
func (c *Cipher) XORKeyStream(dst, src []byte)

XORKeyStream方法将src的数据与秘钥生成的伪随机位流取XOR并写入dst。dst和src可指向同一内存地址;但如果指向不同则其底层内存不可重叠。

Bugs

☞ RC4被广泛使用,但设计上的缺陷使它很少用于较新的协议中。

代码语言:javascript
复制
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package rc4 implements RC4 encryption, as defined in Bruce Schneier's
// Applied Cryptography.
//
// RC4 is cryptographically broken and should not be used for secure
// applications.
package rc4

import (
    "crypto/internal/subtle"
    "strconv"
)

// A Cipher is an instance of RC4 using a particular key.
type Cipher struct {
    s    [256]uint32
    i, j uint8
}

type KeySizeError int

func (k KeySizeError) Error() string {
    return "crypto/rc4: invalid key size " + strconv.Itoa(int(k))
}

// NewCipher creates and returns a new Cipher. The key argument should be the
// RC4 key, at least 1 byte and at most 256 bytes.
func NewCipher(key []byte) (*Cipher, error) {
    k := len(key)
    if k < 1 || k > 256 {
        return nil, KeySizeError(k)
    }
    var c Cipher
    for i := 0; i < 256; i++ {
        c.s[i] = uint32(i)
    }
    var j uint8 = 0
    for i := 0; i < 256; i++ {
        j += uint8(c.s[i]) + key[i%k]
        c.s[i], c.s[j] = c.s[j], c.s[i]
    }
    return &c, nil
}

// Reset zeros the key data and makes the Cipher unusable.
//
// Deprecated: Reset can't guarantee that the key will be entirely removed from
// the process's memory.
func (c *Cipher) Reset() {
    for i := range c.s {
        c.s[i] = 0
    }
    c.i, c.j = 0, 0
}

// XORKeyStream sets dst to the result of XORing src with the key stream.
// Dst and src must overlap entirely or not at all.
func (c *Cipher) XORKeyStream(dst, src []byte) {
    if len(src) == 0 {
        return
    }
    if subtle.InexactOverlap(dst[:len(src)], src) {
        panic("crypto/rc4: invalid buffer overlap")
    }
    i, j := c.i, c.j
    _ = dst[len(src)-1]
    dst = dst[:len(src)] // eliminate bounds check from loop
    for k, v := range src {
        i += 1
        x := c.s[i]
        j += uint8(x)
        y := c.s[j]
        c.s[i], c.s[j] = y, x
        dst[k] = v ^ uint8(c.s[uint8(x+y)])
    }
    c.i, c.j = i, j
}

参考资料:

Go语言中文文档

http://www.golang.ltd/

Go语言官方文档

https://golang.google.cn/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Golang语言情怀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • type KeySizeError
    • func (KeySizeError) Error
    • type Cipher
      • func NewCipher
        • func (*Cipher) Reset
          • func (*Cipher) XORKeyStream
          • Bugs
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档