diff options
author | rsiddharth <s@ricketyspace.net> | 2020-09-06 14:53:07 -0400 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-09-06 14:53:07 -0400 |
commit | 4168c23c8f0f4a2043a65e75d4f8ebe808d8b341 (patch) | |
tree | 7308785ad6621417f4203dfa9e3552793cf718da /lib | |
parent | d20fdad72c7ff1b1b6ee1623626c16ea50284745 (diff) |
lib: add BreakIntoBlocks
* lib/blocks.go (BreakIntoBlocks): New function.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/blocks.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/blocks.go b/lib/blocks.go new file mode 100644 index 0000000..3805d86 --- /dev/null +++ b/lib/blocks.go @@ -0,0 +1,32 @@ +// Copyright © 2020 rsiddharth <s@ricketyspace.net> +// SPDX-License-Identifier: ISC + +package lib + +// Breaks 'cb' into blocks of 'keysize' +func BreakIntoBlocks(cb []byte, keysize int) [][]byte { + if len(cb) < 1 { + return make([][]byte, 0) + } + + // Compute the number of blocks. + nb := len(cb) / keysize + if len(cb)%keysize != 0 { + nb += 1 + } + blocks := make([][]byte, nb) + + for i, j, k := 0, 0, 0; i < len(cb); i++ { + if len(blocks[k]) == 0 { + blocks[k] = make([]byte, keysize) + } + blocks[k][j] = cb[i] + + j += 1 + if j == 8 { + j = 0 + k += 1 + } + } + return blocks +} |