1
0

cipher.go 711 B

1234567891011121314151617181920212223242526272829303132333435
  1. package lightsocks
  2. type Cipher struct {
  3. // 编码用的密码
  4. encodePassword *password
  5. // 解码用的密码
  6. decodePassword *password
  7. }
  8. // 加密原数据
  9. func (cipher *Cipher) Encode(bs []byte) {
  10. for i, v := range bs {
  11. bs[i] = cipher.encodePassword[v]
  12. }
  13. }
  14. // 解码加密后的数据到原数据
  15. func (cipher *Cipher) Decode(bs []byte) {
  16. for i, v := range bs {
  17. bs[i] = cipher.decodePassword[v]
  18. }
  19. }
  20. // 新建一个编码解码器
  21. func NewCipher(encodePassword *password) *Cipher {
  22. decodePassword := &password{}
  23. for i, v := range encodePassword {
  24. encodePassword[i] = v
  25. decodePassword[v] = byte(i)
  26. }
  27. return &Cipher{
  28. encodePassword: encodePassword,
  29. decodePassword: decodePassword,
  30. }
  31. }